home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / dvidoc / dvidoc.web (.txt) < prev    next >
Texinfo Document  |  1994-04-24  |  90KB  |  1,909 lines

  1. % This is DVIDOC, a TeX device driver for text files.  It was written
  2. % at OSU in April, 1983, by modifying the TeX utility DVItype.
  3. % Modified to work under 4.3 BSD by Ken Yap (ken@@cs.rochester.edu)
  4. % Here is TeX material that gets inserted after \input webmac
  5. \def\hang{\hangindent 3em\indent\ignorespaces}
  6. \font\ninerm=cmr9
  7. \let\mc=\ninerm % medium caps for names like SAIL
  8. \def\PASCAL{Pascal}
  9. \def\(#1){} % this is used to make section names sort themselves better
  10. \def\9#1{} % this is used for sort keys in the index
  11. \def\title{DVIDOC}
  12. \def\contentspagenumber{1}
  13. \def\topofcontents{\null
  14.   \def\titlepage{F} % include headline on the contents page
  15.   \def\rheader{\mainfont\hfil \contentspagenumber}
  16.   \vfill
  17.   \centerline{\titlefont The {\ttitlefont DVIDOC} processor}
  18.   \vskip 15pt
  19.   \centerline{(Version 2, July 1987)}
  20.   \vfill}
  21. \def\botofcontents{\vfill
  22.   \centerline{\hsize 5in\baselineskip9pt
  23.     \vbox{\ninerm\noindent
  24.    `\TeX' is a
  25.     trademark of the American Mathematical Society.}}}
  26. \pageno=\contentspagenumber \advance\pageno by 1
  27. @* Introduction.
  28. The \.{DVIDOC} utility program reads binary device-independent (``\.{DVI}'')
  29. files that are produced by document compilers such as \TeX, and 
  30. approximates the intended document as a text file suitable for typing at
  31. a terminal or on a line printer.
  32. This program is based on the program \.{DVItype}, which was written by
  33. Donald Knuth and David Fuchs.
  34. It contained a great deal of code checking for malformed \.{DVI}
  35. files.  Most of that code remains in \.{DVIDOC}, not because it is
  36. important (we trust \TeX) to produce correct \.{DVI} files), but
  37. because is was easier not to disturb the logic in modifying
  38. \.{DVItype} to produce \.{DVIDOC}.
  39. The |banner| string defined here should be changed whenever \.{DVIDOC}
  40. gets modified.
  41. @d banner=='This is DVIDOC, Version 2 for 4.3 BSD' {printed when the program starts}
  42. @ This program is written in standard \PASCAL, except where it is necessary
  43. to use extensions; for example, \.{DVIDOC} must read files whose names
  44. are dynamically specified, and that would be impossible in pure \PASCAL.
  45. All places where nonstandard constructions are used have been listed in
  46. the index under ``system dependencies.''
  47. @!@^system dependencies@>
  48. One of the extensions to standard \PASCAL\ that we shall deal with is the
  49. ability to move to a random place in a binary file.
  50. Another extension is to use a default |case| as in \.{TANGLE}, \.{WEAVE},
  51. @d othercases == others: {default for cases not listed explicitly}
  52. @d endcases == @+end {follows the default case in an extended |case| statement}
  53. @f othercases == else
  54. @f endcases == end
  55. @ The binary input comes from |dvi_file|, and the symbolic output is written
  56. to stdout. The term |print| is used instead of
  57. |write| when this program writes on |err_file|, so that all such output
  58. could easily be redirected if desired.
  59. @d print(#)==write(err_file,#)
  60. @d print_ln(#)==write_ln(err_file,#)
  61. @d error(#)==print_ln(#)
  62. @d doc_file==output
  63. @d eof(#)==eof_dvi_file
  64. @p program DVIDOC(input, output, @!dvi_file,@!err_file);
  65. label @<Labels in the outer block@>@/
  66. const @<Constants in the outer block@>@/
  67. type @<Types in the outer block@>@/
  68. var@?@<Globals in the outer block@>@/
  69. @\@=#include "dvityext.h"@>@\ {declarations for external C procedures}
  70. procedure initialize; {this procedure gets things started properly}
  71. var i:integer;
  72.   begin @/
  73.   setpaths;
  74.   @<Set initial values@>@/
  75.   end;
  76. @ If the program has to stop prematurely, it goes to the
  77. `|final_end|'. Another label, |done|, is used when stopping normally.
  78. @d final_end=9999 {label for the end of it all}
  79. @d done=30 {go here when finished with a subtask}
  80. @<Labels...@>=final_end,done;
  81. @ The following parameters can be changed at compile time to extend or
  82. reduce \.{DVIDOC}'s capacity.
  83. @<Constants...@>=
  84. @!max_fonts=100; {maximum number of distinct fonts per \.{DVI} file}
  85. @!max_widths=10000; {maximum number of different characters among all fonts}
  86. @!terminal_line_length=150; {maximum number of characters input in a single
  87.   line of input from the terminal}
  88. @!stack_size=100; {\.{DVI} files shouldn't |push| beyond this depth}
  89. @!name_size=1000; {total length of all font file names}
  90. @!name_length=100; {a file name shouldn't be longer than this}
  91. @!page_width_max=132; {maximum number of characters per line in the document}
  92. @!page_length_max=88; {maximum number of lines per page in the document}
  93. @ Here are some macros for common programming idioms.
  94. @d incr(#) == #:=#+1 {increase a variable by unity}
  95. @d decr(#) == #:=#-1 {decrease a variable by unity}
  96. @d do_nothing == {empty statement}
  97. @ If the \.{DVI} file is badly malformed, the whole process must be aborted;
  98. \.{DVIDOC} will give up, after issuing an error message about the symptoms
  99. that were noticed.
  100. Such errors might be discovered inside of subroutines inside of subroutines,
  101. so a procedure called |jump_out| has been introduced. This procedure, which
  102. simply transfers control to the label |final_end| at the end of the program,
  103. contains the only non-local |goto| statement in \.{DVIDOC}.
  104. @^system dependencies@>
  105. @d abort(#)==begin message(' ',#); jump_out;
  106.     end
  107. @d bad_dvi(#)==abort('Bad DVI file: ',#,'!')
  108. @.Bad DVI file@>
  109. @p procedure jump_out;
  110. begin goto final_end;
  111. @ If the wrong options have been given to \.{DVIDOC}, we print a reminder
  112. message and exit.
  113. @^system dependencies@>
  114. @d cannot_start(#)==begin message(#); goto done end
  115. @* The character set.
  116. Like all programs written with the  \.{WEB} system, \.{DVIDOC} can be
  117. used with any character set. But it uses ASCII code internally, because
  118. the programming for portable input-output is easier when a fixed internal
  119. code is used, and because \.{DVI} files use ASCII code for file names
  120. and certain other strings.
  121. The next few modules of \.{DVIDOC} have therefore been copied from the
  122. analogous ones in the \.{WEB} system routines. They have been considerably
  123. simplified, since \.{DVIDOC} need not deal with the controversial
  124. ASCII codes less than @'40. If such codes appear in the \.{DVI} file,
  125. they will be printed as question marks.
  126. @<Types...@>=
  127. @!ASCII_code=" ".."~"; {a subrange of the integers}
  128. @ The original \PASCAL\ compiler was designed in the late 60s, when six-bit
  129. character sets were common, so it did not make provision for lower case
  130. letters. Nowadays, of course, we need to deal with both upper and lower case
  131. alphabets in a convenient way, especially in a program like \.{DVIDOC}.
  132. So we shall assume that the \PASCAL\ system being used for \.{DVIDOC}
  133. has a character set containing at least the standard visible characters
  134. of ASCII code (|"!"| through |"~"|).
  135. Some \PASCAL\ compilers use the original name |char| for the data type
  136. associated with the characters in text files, while other \PASCAL s
  137. consider |char| to be a 64-element subrange of a larger data type that has
  138. some other name.  In order to accommodate this difference, we shall use
  139. the name |text_char| to stand for the data type of the characters in the
  140. output file.  We shall also assume that |text_char| consists of
  141. the elements |chr(first_text_char)| through |chr(last_text_char)|,
  142. inclusive. The following definitions should be adjusted if necessary.
  143. @^system dependencies@>
  144. @d text_char == char {the data type of characters in text files}
  145. @d first_text_char=0 {ordinal number of the smallest element of |text_char|}
  146. @d last_text_char=127 {ordinal number of the largest element of |text_char|}
  147. @<Types...@>=
  148. @!text_file=packed file of text_char;
  149. @ The \.{DVIDOC} processor converts between ASCII code and
  150. the user's external character set by means of arrays |xord| and |xchr|
  151. that are analogous to \PASCAL's |ord| and |chr| functions.
  152. @<Globals...@>=
  153. @!xord: array [text_char] of ASCII_code;
  154.   {specifies conversion of input characters}
  155. @!xchr: array [0..255] of text_char;
  156.   {specifies conversion of output characters}
  157. @ Under our assumption that the visible characters of standard ASCII are
  158. all present, the following assignment statements initialize the
  159. |xchr| array properly, without needing any system-dependent changes.
  160. @<Set init...@>=
  161. for i:=0 to @'37 do xchr[i]:='?';
  162. xchr[@'40]:=' ';
  163. xchr[@'41]:='!';
  164. xchr[@'42]:='"';
  165. xchr[@'43]:='#';
  166. xchr[@'44]:='$';
  167. xchr[@'45]:='%';
  168. xchr[@'46]:='&';
  169. xchr[@'47]:='''';@/
  170. xchr[@'50]:='(';
  171. xchr[@'51]:=')';
  172. xchr[@'52]:='*';
  173. xchr[@'53]:='+';
  174. xchr[@'54]:=',';
  175. xchr[@'55]:='-';
  176. xchr[@'56]:='.';
  177. xchr[@'57]:='/';@/
  178. xchr[@'60]:='0';
  179. xchr[@'61]:='1';
  180. xchr[@'62]:='2';
  181. xchr[@'63]:='3';
  182. xchr[@'64]:='4';
  183. xchr[@'65]:='5';
  184. xchr[@'66]:='6';
  185. xchr[@'67]:='7';@/
  186. xchr[@'70]:='8';
  187. xchr[@'71]:='9';
  188. xchr[@'72]:=':';
  189. xchr[@'73]:=';';
  190. xchr[@'74]:='<';
  191. xchr[@'75]:='=';
  192. xchr[@'76]:='>';
  193. xchr[@'77]:='?';@/
  194. xchr[@'100]:='@@';
  195. xchr[@'101]:='A';
  196. xchr[@'102]:='B';
  197. xchr[@'103]:='C';
  198. xchr[@'104]:='D';
  199. xchr[@'105]:='E';
  200. xchr[@'106]:='F';
  201. xchr[@'107]:='G';@/
  202. xchr[@'110]:='H';
  203. xchr[@'111]:='I';
  204. xchr[@'112]:='J';
  205. xchr[@'113]:='K';
  206. xchr[@'114]:='L';
  207. xchr[@'115]:='M';
  208. xchr[@'116]:='N';
  209. xchr[@'117]:='O';@/
  210. xchr[@'120]:='P';
  211. xchr[@'121]:='Q';
  212. xchr[@'122]:='R';
  213. xchr[@'123]:='S';
  214. xchr[@'124]:='T';
  215. xchr[@'125]:='U';
  216. xchr[@'126]:='V';
  217. xchr[@'127]:='W';@/
  218. xchr[@'130]:='X';
  219. xchr[@'131]:='Y';
  220. xchr[@'132]:='Z';
  221. xchr[@'133]:='[';
  222. xchr[@'134]:='\';
  223. xchr[@'135]:=']';
  224. xchr[@'136]:='^';
  225. xchr[@'137]:='_';@/
  226. xchr[@'140]:='`';
  227. xchr[@'141]:='a';
  228. xchr[@'142]:='b';
  229. xchr[@'143]:='c';
  230. xchr[@'144]:='d';
  231. xchr[@'145]:='e';
  232. xchr[@'146]:='f';
  233. xchr[@'147]:='g';@/
  234. xchr[@'150]:='h';
  235. xchr[@'151]:='i';
  236. xchr[@'152]:='j';
  237. xchr[@'153]:='k';
  238. xchr[@'154]:='l';
  239. xchr[@'155]:='m';
  240. xchr[@'156]:='n';
  241. xchr[@'157]:='o';@/
  242. xchr[@'160]:='p';
  243. xchr[@'161]:='q';
  244. xchr[@'162]:='r';
  245. xchr[@'163]:='s';
  246. xchr[@'164]:='t';
  247. xchr[@'165]:='u';
  248. xchr[@'166]:='v';
  249. xchr[@'167]:='w';@/
  250. xchr[@'170]:='x';
  251. xchr[@'171]:='y';
  252. xchr[@'172]:='z';
  253. xchr[@'173]:='{';
  254. xchr[@'174]:='|';
  255. xchr[@'175]:='}';
  256. xchr[@'176]:='~';
  257. for i:=@'177 to 255 do xchr[i]:='?';
  258. @ The following system-independent code makes the |xord| array contain a
  259. suitable inverse to the information in |xchr|.
  260. @<Set init...@>=
  261. for i:=first_text_char to last_text_char do xord[chr(i)]:=@'40;
  262. for i:=" " to "~" do xord[xchr[i]]:=i;
  263. @* Device-independent file format.
  264. Before we get into the details of \.{DVItype}, we need to know exactly
  265. what \.{DVI} files are. The form of such files was designed by David R.
  266. @^Fuchs, David Raymond@>
  267. Fuchs in 1979. Almost any reasonable typesetting device can be driven by
  268. a program that takes \.{DVI} files as input, and dozens of such
  269. \.{DVI}-to-whatever programs have been written. Thus, it is possible to
  270. print the output of document compilers like \TeX\ on many different kinds
  271. of equipment.
  272. A \.{DVI} file is a stream of 8-bit bytes, which may be regarded as a
  273. series of commands in a machine-like language. The first byte of each command
  274. is the operation code, and this code is followed by zero or more bytes
  275. that provide parameters to the command. The parameters themselves may consist
  276. of several consecutive bytes; for example, the `|set_rule|' command has two
  277. parameters, each of which is four bytes long. Parameters are usually
  278. regarded as nonnegative integers; but four-byte-long parameters,
  279. and shorter parameters that denote distances, can be
  280. either positive or negative. Such parameters are given in two's complement
  281. notation. For example, a two-byte-long distance parameter has a value between
  282. $-2^{15}$ and $2^{15}-1$.
  283. @.DVI {\rm files}@>
  284. A \.{DVI} file consists of a ``preamble,'' followed by a sequence of one
  285. or more ``pages,'' followed by a ``postamble.'' The preamble is simply a
  286. |pre| command, with its parameters that define the dimensions used in the
  287. file; this must come first.  Each ``page'' consists of a |bop| command,
  288. followed by any number of other commands that tell where characters are to
  289. be placed on a physical page, followed by an |eop| command. The pages
  290. appear in the order that they were generated, not in any particular
  291. numerical order. If we ignore |nop| commands and \\{fnt\_def} commands
  292. (which are allowed between any two commands in the file), each |eop|
  293. command is immediately followed by a |bop| command, or by a |post|
  294. command; in the latter case, there are no more pages in the file, and the
  295. remaining bytes form the postamble.  Further details about the postamble
  296. will be explained later.
  297. Some parameters in \.{DVI} commands are ``pointers.'' These are four-byte
  298. quantities that give the location number of some other byte in the file;
  299. the first byte is number~0, then comes number~1, and so on. For example,
  300. one of the parameters of a |bop| command points to the previous |bop|;
  301. this makes it feasible to read the pages in backwards order, in case the
  302. results are being directed to a device that stacks its output face up.
  303. Suppose the preamble of a \.{DVI} file occupies bytes 0 to 99. Now if the
  304. first page occupies bytes 100 to 999, say, and if the second
  305. page occupies bytes 1000 to 1999, then the |bop| that starts in byte 1000
  306. points to 100 and the |bop| that starts in byte 2000 points to 1000. (The
  307. very first |bop|, i.e., the one that starts in byte 100, has a pointer of $-1$.)
  308. @ The \.{DVI} format is intended to be both compact and easily interpreted
  309. by a machine. Compactness is achieved by making most of the information
  310. implicit instead of explicit. When a \.{DVI}-reading program reads the
  311. commands for a page, it keeps track of several quantities: (a)~The current
  312. font |f| is an integer; this value is changed only
  313. by \\{fnt} and \\{fnt\_num} commands. (b)~The current position on the page
  314. is given by two numbers called the horizontal and vertical coordinates,
  315. |h| and |v|. Both coordinates are zero at the upper left corner of the page;
  316. moving to the right corresponds to increasing the horizontal coordinate, and
  317. moving down corresponds to increasing the vertical coordinate. Thus, the
  318. coordinates are essentially Cartesian, except that vertical directions are
  319. flipped; the Cartesian version of |(h,v)| would be |(h,-v)|.  (c)~The
  320. current spacing amounts are given by four numbers |w|, |x|, |y|, and |z|,
  321. where |w| and~|x| are used for horizontal spacing and where |y| and~|z|
  322. are used for vertical spacing. (d)~There is a stack containing
  323. |(h,v,w,x,y,z)| values; the \.{DVI} commands |push| and |pop| are used to
  324. change the current level of operation. Note that the current font~|f| is
  325. not pushed and popped; the stack contains only information about
  326. positioning.
  327. The values of |h|, |v|, |w|, |x|, |y|, and |z| are signed integers having up
  328. to 32 bits, including the sign. Since they represent physical distances,
  329. there is a small unit of measurement such that increasing |h| by~1 means
  330. moving a certain tiny distance to the right. The actual unit of
  331. measurement is variable, as explained below.
  332. @ Here is a list of all the commands that may appear in a \.{DVI} file. Each
  333. command is specified by its symbolic name (e.g., |bop|), its opcode byte
  334. (e.g., 139), and its parameters (if any). The parameters are followed
  335. by a bracketed number telling how many bytes they occupy; for example,
  336. `|p[4]|' means that parameter |p| is four bytes long.
  337. \yskip\hang|set_char_0| 0. Typeset character number~0 from font~|f|
  338. such that the reference point of the character is at |(h,v)|. Then
  339. increase |h| by the width of that character. Note that a character may
  340. have zero or negative width, so one cannot be sure that |h| will advance
  341. after this command; but |h| usually does increase.
  342. \yskip\hang|set_char_1| through |set_char_127| (opcodes 1 to 127).
  343. Do the operations of |set_char_0|; but use the character whose number
  344. matches the opcode, instead of character~0.
  345. \yskip\hang|set1| 128 |c[1]|. Same as |set_char_0|, except that character
  346. number~|c| is typeset. \TeX82 uses this command for characters in the
  347. range |128<=c<256|.
  348. \yskip\hang|set2| 129 |c[2]|. Same as |set1|, except that |c|~is two
  349. bytes long, so it is in the range |0<=c<65536|. \TeX82 never uses this
  350. command, which is intended for processors that deal with oriental languages;
  351. but \.{DVItype} will allow character codes greater than 255, assuming that
  352. they all have the same width as the character whose code is $c \bmod 256$.
  353. @^oriental characters@>@^Chinese characters@>@^Japanese characters@>
  354. \yskip\hang|set3| 130 |c[3]|. Same as |set1|, except that |c|~is three
  355. bytes long, so it can be as large as $2^{24}-1$.
  356. \yskip\hang|set4| 131 |c[4]|. Same as |set1|, except that |c|~is four
  357. bytes long, possibly even negative. Imagine that.
  358. \yskip\hang|set_rule| 132 |a[4]| |b[4]|. Typeset a solid black rectangle
  359. of height |a| and width |b|, with its bottom left corner at |(h,v)|. Then
  360. set |h:=h+b|. If either |a<=0| or |b<=0|, nothing should be typeset. Note
  361. that if |b<0|, the value of |h| will decrease even though nothing else happens.
  362. Programs that typeset from \.{DVI} files should be careful to make the rules
  363. line up carefully with digitized characters, as explained in connection with
  364. the |rule_pixels| subroutine below.
  365. \yskip\hang|put1| 133 |c[1]|. Typeset character number~|c| from font~|f|
  366. such that the reference point of the character is at |(h,v)|. (The `put'
  367. commands are exactly like the `set' commands, except that they simply put out a
  368. character or a rule without moving the reference point afterwards.)
  369. \yskip\hang|put2| 134 |c[2]|. Same as |set2|, except that |h| is not changed.
  370. \yskip\hang|put3| 135 |c[3]|. Same as |set3|, except that |h| is not changed.
  371. \yskip\hang|put4| 136 |c[4]|. Same as |set4|, except that |h| is not changed.
  372. \yskip\hang|put_rule| 137 |a[4]| |b[4]|. Same as |set_rule|, except that
  373. |h| is not changed.
  374. \yskip\hang|nop| 138. No operation, do nothing. Any number of |nop|'s
  375. may occur between \.{DVI} commands, but a |nop| cannot be inserted between
  376. a command and its parameters or between two parameters.
  377. \yskip\hang|bop| 139 $c_0[4]$ $c_1[4]$ $\ldots$ $c_9[4]$ $p[4]$. Beginning
  378. of a page: Set |(h,v,w,x,y,z):=(0,0,0,0,0,0)| and set the stack empty. Set
  379. the current font |f| to an undefined value.  The ten $c_i$ parameters can
  380. be used to identify pages, if a user wants to print only part of a \.{DVI}
  381. file; \TeX82 gives them the values of \.{\\count0} $\ldots$ \.{\\count9}
  382. at the time \.{\\shipout} was invoked for this page.  The parameter |p|
  383. points to the previous |bop| command in the file, where the first |bop|
  384. has $p=-1$.
  385. \yskip\hang|eop| 140.  End of page: Print what you have read since the
  386. previous |bop|. At this point the stack should be empty. (The \.{DVI}-reading
  387. programs that drive most output devices will have kept a buffer of the
  388. material that appears on the page that has just ended. This material is
  389. largely, but not entirely, in order by |v| coordinate and (for fixed |v|) by
  390. |h|~coordinate; so it usually needs to be sorted into some order that is
  391. appropriate for the device in question. \.{DVItype} does not do such sorting.)
  392. \yskip\hang|push| 141. Push the current values of |(h,v,w,x,y,z)| onto the
  393. top of the stack; do not change any of these values. Note that |f| is
  394. not pushed.
  395. \yskip\hang|pop| 142. Pop the top six values off of the stack and assign
  396. them to |(h,v,w,x,y,z)|. The number of pops should never exceed the number
  397. of pushes, since it would be highly embarrassing if the stack were empty
  398. at the time of a |pop| command.
  399. \yskip\hang|right1| 143 |b[1]|. Set |h:=h+b|, i.e., move right |b| units.
  400. The parameter is a signed number in two's complement notation, |-128<=b<128|;
  401. if |b<0|, the reference point actually moves left.
  402. \yskip\hang|right2| 144 |b[2]|. Same as |right1|, except that |b| is a
  403. two-byte quantity in the range |-32768<=b<32768|.
  404. \yskip\hang|right3| 145 |b[3]|. Same as |right1|, except that |b| is a
  405. three-byte quantity in the range |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
  406. \yskip\hang|right4| 146 |b[4]|. Same as |right1|, except that |b| is a
  407. four-byte quantity in the range |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
  408. \yskip\hang|w0| 147. Set |h:=h+w|; i.e., move right |w| units. With luck,
  409. this parameterless command will usually suffice, because the same kind of motion
  410. will occur several times in succession; the following commands explain how
  411. |w| gets particular values.
  412. \yskip\hang|w1| 148 |b[1]|. Set |w:=b| and |h:=h+b|. The value of |b| is a
  413. signed quantity in two's complement notation, |-128<=b<128|. This command
  414. changes the current |w|~spacing and moves right by |b|.
  415. \yskip\hang|w2| 149 |b[2]|. Same as |w1|, but |b| is a two-byte-long
  416. parameter, |-32768<=b<32768|.
  417. \yskip\hang|w3| 150 |b[3]|. Same as |w1|, but |b| is a three-byte-long
  418. parameter, |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
  419. \yskip\hang|w4| 151 |b[4]|. Same as |w1|, but |b| is a four-byte-long
  420. parameter, |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
  421. \yskip\hang|x0| 152. Set |h:=h+x|; i.e., move right |x| units. The `|x|'
  422. commands are like the `|w|' commands except that they involve |x| instead
  423. of |w|.
  424. \yskip\hang|x1| 153 |b[1]|. Set |x:=b| and |h:=h+b|. The value of |b| is a
  425. signed quantity in two's complement notation, |-128<=b<128|. This command
  426. changes the current |x|~spacing and moves right by |b|.
  427. \yskip\hang|x2| 154 |b[2]|. Same as |x1|, but |b| is a two-byte-long
  428. parameter, |-32768<=b<32768|.
  429. \yskip\hang|x3| 155 |b[3]|. Same as |x1|, but |b| is a three-byte-long
  430. parameter, |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
  431. \yskip\hang|x4| 156 |b[4]|. Same as |x1|, but |b| is a four-byte-long
  432. parameter, |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
  433. \yskip\hang|down1| 157 |a[1]|. Set |v:=v+a|, i.e., move down |a| units.
  434. The parameter is a signed number in two's complement notation, |-128<=a<128|;
  435. if |a<0|, the reference point actually moves up.
  436. \yskip\hang|down2| 158 |a[2]|. Same as |down1|, except that |a| is a
  437. two-byte quantity in the range |-32768<=a<32768|.
  438. \yskip\hang|down3| 159 |a[3]|. Same as |down1|, except that |a| is a
  439. three-byte quantity in the range |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
  440. \yskip\hang|down4| 160 |a[4]|. Same as |down1|, except that |a| is a
  441. four-byte quantity in the range |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
  442. \yskip\hang|y0| 161. Set |v:=v+y|; i.e., move down |y| units. With luck,
  443. this parameterless command will usually suffice, because the same kind of motion
  444. will occur several times in succession; the following commands explain how
  445. |y| gets particular values.
  446. \yskip\hang|y1| 162 |a[1]|. Set |y:=a| and |v:=v+a|. The value of |a| is a
  447. signed quantity in two's complement notation, |-128<=a<128|. This command
  448. changes the current |y|~spacing and moves down by |a|.
  449. \yskip\hang|y2| 163 |a[2]|. Same as |y1|, but |a| is a two-byte-long
  450. parameter, |-32768<=a<32768|.
  451. \yskip\hang|y3| 164 |a[3]|. Same as |y1|, but |a| is a three-byte-long
  452. parameter, |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
  453. \yskip\hang|y4| 165 |a[4]|. Same as |y1|, but |a| is a four-byte-long
  454. parameter, |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
  455. \yskip\hang|z0| 166. Set |v:=v+z|; i.e., move down |z| units. The `|z|' commands
  456. are like the `|y|' commands except that they involve |z| instead of |y|.
  457. \yskip\hang|z1| 167 |a[1]|. Set |z:=a| and |v:=v+a|. The value of |a| is a
  458. signed quantity in two's complement notation, |-128<=a<128|. This command
  459. changes the current |z|~spacing and moves down by |a|.
  460. \yskip\hang|z2| 168 |a[2]|. Same as |z1|, but |a| is a two-byte-long
  461. parameter, |-32768<=a<32768|.
  462. \yskip\hang|z3| 169 |a[3]|. Same as |z1|, but |a| is a three-byte-long
  463. parameter, |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
  464. \yskip\hang|z4| 170 |a[4]|. Same as |z1|, but |a| is a four-byte-long
  465. parameter, |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
  466. \yskip\hang|fnt_num_0| 171. Set |f:=0|. Font 0 must previously have been
  467. defined by a \\{fnt\_def} instruction, as explained below.
  468. \yskip\hang|fnt_num_1| through |fnt_num_63| (opcodes 172 to 234). Set
  469. |f:=1|, \dots, |f:=63|, respectively.
  470. \yskip\hang|fnt1| 235 |k[1]|. Set |f:=k|. \TeX82 uses this command for font
  471. numbers in the range |64<=k<256|.
  472. \yskip\hang|fnt2| 236 |k[2]|. Same as |fnt1|, except that |k|~is two
  473. bytes long, so it is in the range |0<=k<65536|. \TeX82 never generates this
  474. command, but large font numbers may prove useful for specifications of
  475. color or texture, or they may be used for special fonts that have fixed
  476. numbers in some external coding scheme.
  477. \yskip\hang|fnt3| 237 |k[3]|. Same as |fnt1|, except that |k|~is three
  478. bytes long, so it can be as large as $2^{24}-1$.
  479. \yskip\hang|fnt4| 238 |k[4]|. Same as |fnt1|, except that |k|~is four
  480. bytes long; this is for the really big font numbers (and for the negative ones).
  481. \yskip\hang|xxx1| 239 |k[1]| |x[k]|. This command is undefined in
  482. general; it functions as a $(k+2)$-byte |nop| unless special \.{DVI}-reading
  483. programs are being used. \TeX82 generates |xxx1| when a short enough
  484. \.{\\special} appears, setting |k| to the number of bytes being sent. It
  485. is recommended that |x| be a string having the form of a keyword followed
  486. by possible parameters relevant to that keyword.
  487. \yskip\hang|xxx2| 240 |k[2]| |x[k]|. Like |xxx1|, but |0<=k<65536|.
  488. \yskip\hang|xxx3| 241 |k[3]| |x[k]|. Like |xxx1|, but |0<=k<@t$2^{24}$@>|.
  489. \yskip\hang|xxx4| 242 |k[4]| |x[k]|. Like |xxx1|, but |k| can be ridiculously
  490. large. \TeX82 uses |xxx4| when |xxx1| would be incorrect.
  491. \yskip\hang|fnt_def1| 243 |k[1]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  492. Define font |k|, where |0<=k<256|; font definitions will be explained shortly.
  493. \yskip\hang|fnt_def2| 244 |k[2]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  494. Define font |k|, where |0<=k<65536|.
  495. \yskip\hang|fnt_def3| 245 |k[3]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  496. Define font |k|, where |0<=k<@t$2^{24}$@>|.
  497. \yskip\hang|fnt_def4| 246 |k[4]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  498. Define font |k|, where |@t$-2^{31}$@><=k<@t$2^{31}$@>|.
  499. \yskip\hang|pre| 247 |i[1]| |num[4]| |den[4]| |mag[4]| |k[1]| |x[k]|.
  500. Beginning of the preamble; this must come at the very beginning of the
  501. file. Parameters |i|, |num|, |den|, |mag|, |k|, and |x| are explained below.
  502. \yskip\hang|post| 248. Beginning of the postamble, see below.
  503. \yskip\hang|post_post| 249. Ending of the postamble, see below.
  504. \yskip\noindent Commands 250--255 are undefined at the present time.
  505. @ @d set_char_0=0 {typeset character 0 and move right}
  506. @d set1=128 {typeset a character and move right}
  507. @d set_rule=132 {typeset a rule and move right}
  508. @d put1=133 {typeset a character}
  509. @d put_rule=137 {typeset a rule}
  510. @d nop=138 {no operation}
  511. @d bop=139 {beginning of page}
  512. @d eop=140 {ending of page}
  513. @d push=141 {save the current positions}
  514. @d pop=142 {restore previous positions}
  515. @d right1=143 {move right}
  516. @d w0=147 {move right by |w|}
  517. @d w1=148 {move right and set |w|}
  518. @d x0=152 {move right by |x|}
  519. @d x1=153 {move right and set |x|}
  520. @d down1=157 {move down}
  521. @d y0=161 {move down by |y|}
  522. @d y1=162 {move down and set |y|}
  523. @d z0=166 {move down by |z|}
  524. @d z1=167 {move down and set |z|}
  525. @d fnt_num_0=171 {set current font to 0}
  526. @d fnt1=235 {set current font}
  527. @d xxx1=239 {extension to \.{DVI} primitives}
  528. @d xxx4=242 {potentially long extension to \.{DVI} primitives}
  529. @d fnt_def1=243 {define the meaning of a font number}
  530. @d pre=247 {preamble}
  531. @d post=248 {postamble beginning}
  532. @d post_post=249 {postamble ending}
  533. @d undefined_commands==250,251,252,253,254,255
  534. @ The preamble contains basic information about the file as a whole. As
  535. stated above, there are six parameters:
  536. $$\hbox{|@!i[1]| |@!num[4]| |@!den[4]| |@!mag[4]| |@!k[1]| |@!x[k]|.}$$
  537. The |i| byte identifies \.{DVI} format; currently this byte is always set
  538. to~2. (Some day we will set |i=3|, when \.{DVI} format makes another
  539. incompatible change---perhaps in 1992.)
  540. The next two parameters, |num| and |den|, are positive integers that define
  541. the units of measurement; they are the numerator and denominator of a
  542. fraction by which all dimensions in the \.{DVI} file could be multiplied
  543. in order to get lengths in units of $10^{-7}$ meters. (For example, there are
  544. exactly 7227 \TeX\ points in 254 centimeters, and \TeX82 works with scaled
  545. points where there are $2^{16}$ sp in a point, so \TeX82 sets |num=25400000|
  546. and $|den|=7227\cdot2^{16}=473628672$.)
  547. @^sp@>
  548. The |mag| parameter is what \TeX82 calls \.{\\mag}, i.e., 1000 times the
  549. desired magnification. The actual fraction by which dimensions are
  550. multiplied is therefore $mn/1000d$. Note that if a \TeX\ source document
  551. does not call for any `\.{true}' dimensions, and if you change it only by
  552. specifying a different \.{\\mag} setting, the \.{DVI} file that \TeX\
  553. creates will be completely unchanged except for the value of |mag| in the
  554. preamble and postamble. (Fancy \.{DVI}-reading programs allow users to
  555. override the |mag|~setting when a \.{DVI} file is being printed.)
  556. Finally, |k| and |x| allow the \.{DVI} writer to include a comment, which is not
  557. interpreted further. The length of comment |x| is |k|, where |0<=k<256|.
  558. @d id_byte=2 {identifies the kind of \.{DVI} files described here}
  559. @ Font definitions for a given font number |k| contain further parameters
  560. $$\hbox{|c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.}$$
  561. The four-byte value |c| is the check sum that \TeX\ (or whatever program
  562. generated the \.{DVI} file) found in the \.{TFM} file for this font;
  563. |c| should match the check sum of the font found by programs that read
  564. this \.{DVI} file.
  565. @^check sum@>
  566. Parameter |s| contains a fixed-point scale factor that is applied to the
  567. character widths in font |k|; font dimensions in \.{TFM} files and other
  568. font files are relative to this quantity, which is always positive and
  569. less than $2^{27}$. It is given in the same units as the other dimensions
  570. of the \.{DVI} file.  Parameter |d| is similar to |s|; it is the ``design
  571. size,'' and it is given in \.{DVI} units that have not been corrected for
  572. the magnification~|mag| found in the preamble.  Thus, font |k| is to be
  573. used at $|mag|\cdot s/1000d$ times its normal size.
  574. The remaining part of a font definition gives the external name of the font,
  575. which is an ASCII string of length |a+l|. The number |a| is the length
  576. of the ``area'' or directory, and |l| is the length of the font name itself;
  577. the standard local system font area is supposed to be used when |a=0|.
  578. The |n| field contains the area in its first |a| bytes.
  579. Font definitions must appear before the first use of a particular font number.
  580. Once font |k| is defined, it must not be defined again; however, we
  581. shall see below that font definitions appear in the postamble as well as
  582. in the pages, so in this sense each font number is defined exactly twice,
  583. if at all. Like |nop| commands and \\{xxx} commands, font definitions can
  584. appear before the first |bop|, or between an |eop| and a |bop|.
  585. @ The last page in a \.{DVI} file is followed by `|post|'; this command
  586. introduces the postamble, which summarizes important facts that \TeX\ has
  587. accumulated about the file, making it possible to print subsets of the data
  588. with reasonable efficiency. The postamble has the form
  589. $$\vbox{\halign{\hbox{#\hfil}\cr
  590.   |post| |p[4]| |num[4]| |den[4]| |mag[4]| |l[4]| |u[4]| |s[2]| |t[2]|\cr
  591.   $\langle\,$font definitions$\,\rangle$\cr
  592.   |post_post| |q[4]| |i[1]| 223's$[{\G}4]$\cr}}$$
  593. Here |p| is a pointer to the final |bop| in the file. The next three
  594. parameters, |num|, |den|, and |mag|, are duplicates of the quantities that
  595. appeared in the preamble.
  596. Parameters |l| and |u| give respectively the height-plus-depth of the tallest
  597. page and the width of the widest page, in the same units as other dimensions
  598. of the file. These numbers might be used by a \.{DVI}-reading program to
  599. position individual ``pages'' on large sheets of film or paper; however,
  600. the standard convention for output on normal size paper is to position each
  601. page so that the upper left-hand corner is exactly one inch from the left
  602. and the top. Experience has shown that it is unwise to design \.{DVI}-to-printer
  603. software that attempts cleverly to center the output; a fixed position of
  604. the upper left corner is easiest for users to understand and to work with.
  605. Therefore |l| and~|u| are often ignored.
  606. Parameter |s| is the maximum stack depth (i.e., the largest excess of
  607. |push| commands over |pop| commands) needed to process this file. Then
  608. comes |t|, the total number of pages (|bop| commands) present.
  609. The postamble continues with font definitions, which are any number of
  610. \\{fnt\_def} commands as described above, possibly interspersed with |nop|
  611. commands. Each font number that is used in the \.{DVI} file must be defined
  612. exactly twice: Once before it is first selected by a \\{fnt} command, and once
  613. in the postamble.
  614. @ The last part of the postamble, following the |post_post| byte that
  615. signifies the end of the font definitions, contains |q|, a pointer to the
  616. |post| command that started the postamble.  An identification byte, |i|,
  617. comes next; this currently equals~2, as in the preamble.
  618. The |i| byte is followed by four or more bytes that are all equal to
  619. the decimal number 223 (i.e., @'337 in octal). \TeX\ puts out four to seven of
  620. these trailing bytes, until the total length of the file is a multiple of
  621. four bytes, since this works out best on machines that pack four bytes per
  622. word; but any number of 223's is allowed, as long as there are at least four
  623. of them. In effect, 223 is a sort of signature that is added at the very end.
  624. @^Fuchs, David Raymond@>
  625. This curious way to finish off a \.{DVI} file makes it feasible for
  626. \.{DVI}-reading programs to find the postamble first, on most computers,
  627. even though \TeX\ wants to write the postamble last. Most operating
  628. systems permit random access to individual words or bytes of a file, so
  629. the \.{DVI} reader can start at the end and skip backwards over the 223's
  630. until finding the identification byte. Then it can back up four bytes, read
  631. |q|, and move to byte |q| of the file. This byte should, of course,
  632. contain the value 248 (|post|); now the postamble can be read, so the
  633. \.{DVI} reader discovers all the information needed for typesetting the
  634. pages. Note that it is also possible to skip through the \.{DVI} file at
  635. reasonably high speed to locate a particular page, if that proves
  636. desirable. This saves a lot of time, since \.{DVI} files used in production
  637. jobs tend to be large.
  638. When \.{DVIDOC} "typesets" a character, it simply puts its ASCII code
  639. into the document file in the proper place according to the rounding of
  640. |h| and |v| to whole character positions.  It may, of course, obliterate
  641. a character previously stored in the same position.  Especially if a
  642. symbol font is being used, the ASCII code may print ultimately as an
  643. entirely different character than the one the document designer originally
  644. intended.  For \.{DVIDOC} to produce more than a rough approximation to 
  645. the intended document, fonts need to be chosen very carefully.
  646. @* Input from binary files.
  647. We have seen that a \.{DVI} file is a sequence of 8-bit bytes. The bytes
  648. appear physically in what is called a `|packed file of 0..255|'
  649. in \PASCAL\ lingo.
  650. Packing is system dependent, and many \PASCAL\ systems fail to implement
  651. such files in a sensible way (at least, from the viewpoint of producing
  652. good production software).  For example, some systems treat all
  653. byte-oriented files as text, looking for end-of-line marks and such
  654. things. Therefore some system-dependent code is often needed to deal with
  655. binary files, even though most of the program in this section of
  656. \.{DVIDOC} is written in standard \PASCAL.
  657. @^system dependencies@>
  658. One common way to solve the problem is to consider files of |integer|
  659. numbers, and to convert an integer in the range $-2^{31}\L x<2^{31}$ to
  660. a sequence of four bytes $(a,b,c,d)$ using the following code, which
  661. avoids the controversial integer division of negative numbers:
  662. $$\vbox{\halign{#\hfil\cr
  663. |if x>=0 then a:=x div @'100000000|\cr
  664. |else begin x:=(x+@'10000000000)+@'10000000000; a:=x div @'100000000+128;|\cr
  665. \quad|end|\cr
  666. |x:=x mod @'100000000;|\cr
  667. |b:=x div @'200000; x:=x mod @'200000;|\cr
  668. |c:=x div @'400; d:=x mod @'400;|\cr}}$$
  669. The four bytes are then kept in a buffer and output one by one. (On 36-bit
  670. computers, an additional division by 16 is necessary at the beginning.
  671. Another way to separate an integer into four bytes is to use/abuse
  672. \PASCAL's variant records, storing an integer and retrieving bytes that are
  673. packed in the same place; {\sl caveat implementor!\/}) It is also desirable
  674. in some cases to read a hundred or so integers at a time, maintaining a
  675. larger buffer.
  676. We shall stick to simple \PASCAL\ in this program, for reasons of clarity,
  677. even if such simplicity is sometimes unrealistic.
  678. @<Types...@>=
  679. @!eight_bits=integer; {unsigned one-byte quantity}
  680. @!byte_file=packed file of char; {files that contain binary data}
  681. @ The program deals with two binary file variables: |dvi_file| is the main
  682. input file that we are translating into symbolic form, and |tfm_file| is
  683. the current font metric file from which character-width information is
  684. being read.
  685. @<Glob...@>=
  686. @!dvi_file:byte_file; {the stuff we are \.{DVI}typing}
  687. @!eof_dvi_file:boolean; {end of file on \.{DVI}file}
  688. @!tfm_file:byte_file; {a font metric file}
  689. @ To prepare these files for input, we |reset| them. An extension of
  690. \PASCAL\ is needed in the case of |tfm_file|, since we want to associate
  691. it with external files whose names are specified dynamically (i.e., not
  692. known at compile time). The following code assumes that `|reset(f,s)|'
  693. does this, when |f| is a file variable and |s| is a string variable that
  694. specifies the file name. If |eof(f)| is true immediately after
  695. |reset(f,s)| has acted, we assume that no file named |s| is accessible.
  696. @^system dependencies@>
  697. @d read_access_mode=4  {``read'' mode for |test_access|}
  698. @d write_access_mode=2 {``write'' mode for |test_access|}
  699. @d no_file_path=0    {no path searching should be done}
  700. @d font_file_path=3  {path specifier for \.{TFM} files}
  701. @ We try to open the input file and append .dvi if it doesn't have this
  702. extension already.
  703. @^system dependencies@>
  704. @<Open input file@>=
  705. i := 1;
  706. while (i < name_length) and (base_name[i] <> ' ') do incr(i);
  707. if i < 5 then begin
  708.     base_name[i] := '.'; base_name[i+1] := 'd'; base_name[i+2] := 'v';
  709.     base_name[i+3] := 'i'; base_name[i+4] := ' ';
  710.     end;
  711. if (i >= 5) and (i < name_length - 4) and ((base_name[i-4] <> '.') or
  712.     (base_name[i-3] <> 'd') or (base_name[i-2] <> 'v') or
  713.     (base_name[i-1] <> 'i')) then begin
  714.     base_name[i] := '.'; base_name[i+1] := 'd'; base_name[i+2] := 'v';
  715.     base_name[i+3] := 'i'; base_name[i+4] := ' ';
  716.     end;
  717. cur_name := base_name;
  718. if test_access(read_access_mode, no_file_path) then begin
  719.     open_dvi_file;
  720.     if eof(dvi_file) then
  721.         cannot_start('Input file not found')
  722.     end
  723.     cannot_start('Cannot open input file');
  724. @ Procedure to open TFM file.
  725. @p procedure open_tfm_file; {prepares to read packed bytes in |tfm_file|}
  726. begin 
  727. if test_access(read_access_mode, font_file_path) then 
  728.     reset(tfm_file, real_name_of_file) 
  729. else begin
  730.     error('TFM file not found');
  731.     goto done;
  732.     end;
  733. @ If you looked carefully at the preceding code, you probably asked,
  734. ``What are |cur_loc| and |cur_name|?'' Good question. They're global
  735. variables: |cur_loc| is the number of the byte about to be read next from
  736. |dvi_file|, and |cur_name| is a string variable that will be set to the
  737. current font metric file name before |open_tfm_file| is called.
  738. @<Glob...@>=
  739. @!cur_loc:integer; {where we are about to look, in |dvi_file|}
  740. @!base_name,cur_name,real_name_of_file:
  741. packed array[1..name_length] of char; {external name,
  742.   with no upper case letters}
  743. @ It turns out to be convenient to read four bytes at a time, when we are
  744. inputting from \.{TFM} files. The input goes into global variables
  745. |b0|, |b1|, |b2|, and |b3|, with |b0| getting the first byte and |b3|
  746. the fourth.
  747. @<Glob...@>=
  748. @!b0,@!b1,@!b2,@!b3: eight_bits; {four bytes input at once}
  749. @ The |read_tfm_word| procedure sets |b0| through |b3| to the next
  750. four bytes in the current \.{TFM} file.
  751. @d get_tfm_byte(#) ==
  752. @^system dependencies@>
  753.     read(tfm_file,byte); # := ord(byte);
  754.     if # < 0 then # := # + 256;
  755. @p procedure read_tfm_word;
  756. var byte:char;
  757. begin
  758.     get_tfm_byte(b0); get_tfm_byte(b1); get_tfm_byte(b2); get_tfm_byte(b3);
  759. @ Finally we come to the routines that are used for random reading.
  760. The driver program below needs two such routines: |dvi_length| should
  761. compute the total number of bytes in |dvi_file|, possibly also
  762. causing |eof(dvi_file)| to be true; and |move_to_byte(n)|
  763. should position |dvi_file| so that the next |get_byte| will read byte |n|,
  764. starting with |n=0| for the first byte in the file.
  765. @^system dependencies@>
  766. Such routines are, of course, highly system dependent. They are implemented
  767. here in terms of two assumed system routines called |set_pos| and |cur_pos|.
  768. The call |set_pos(f,n)| moves to item |n| in file |f|, unless |n| is
  769. negative or larger than the total number of items in |f|; in the latter
  770. case, |set_pos(f,n)| moves to the end of file |f|.
  771. The call |cur_pos(f)| gives the total number of items in |f|, if
  772. |eof(f)| is true; we use |cur_pos| only in such a situation.
  773. @p function dvi_length:integer;
  774. begin set_pos(dvi_file,-1); dvi_length:=cur_pos(dvi_file);
  775. procedure move_to_byte(n:integer);
  776. begin set_pos(dvi_file,n); cur_loc:=n;
  777. @* Reading the font information.
  778. \.{DVI} file format does not include information about character widths, since
  779. that would tend to make the files a lot longer. But a program that reads
  780. a \.{DVI} file is supposed to know the widths of the characters that appear
  781. in \\{set\_char} commands. Therefore \.{DVIDOC} looks at the font metric
  782. (\.{TFM}) files for the fonts that are involved.
  783. @.TFM {\rm files}@>
  784. The character-width data appears also in other files (e.g., in \.{GF} files
  785. that specify bit patterns for digitized characters);
  786. thus, it is usually possible for \.{DVI} reading programs to get by with
  787. accessing only one file per font. \.{DVItype} has a comparatively easy
  788. task in this regard, since it needs only a few words of information from
  789. each font; other \.{DVI}-to-printer programs may have to go to some pains to
  790. deal with complications that arise when a large number of large font files
  791. all need to be accessed simultaneously.
  792. @ For purposes of this program, we need to know only two things about a
  793. given character |c| in a given font |f|: (1)~Is |c| a legal character
  794. in~|f|? (2)~If so, what is the width of |c|? We also need to know the
  795. symbolic name of each font, so it can be printed out, and we need to know
  796. the approximate size of inter-word spaces in each font.
  797. The answers to these questions appear implicitly in the following data
  798. structures. The current number of known fonts is |nf|. Each known font has
  799. an internal number |f|, where |0<=f<nf|; the external number of this font,
  800. i.e., its font identification number in the \.{DVI} file, is
  801. |font_num[f]|, and the external name of this font is the string that
  802. occupies positions |font_name[f]| through |font_name[f+1]-1| of the array
  803. |names|. The latter array consists of |ASCII_code| characters, and
  804. |font_name[nf]| is its first unoccupied position.  A horizontal motion
  805. in the range |-4*font_space[f]<h<font_space[f]|
  806. will be treated as a `kern' that is not
  807. indicated in the printouts that \.{DVIDOC} produces between brackets. The
  808. legal characters run from |font_bc[f]| to |font_ec[f]|, inclusive; more
  809. precisely, a given character |c| is valid in font |f| if and only if
  810. |font_bc[f]<=c<=font_ec[f]| and |char_width(f)(c)<>invalid_width|.
  811. Finally, |char_width(f)(c)=width[width_base[f]+c]|, and |width_ptr| is the
  812. first unused position of the |width| array.
  813. @d char_width_end(#)==#]
  814. @d char_width(#)==width[width_base[#]+char_width_end
  815. @d invalid_width==@'17777777777
  816. @<Glob...@>=
  817. @!font_num:array [0..max_fonts] of integer; {external font numbers}
  818. @!font_name:array [0..max_fonts] of 0..name_size; {starting positions
  819.   of external font names}
  820. @!names:array [0..name_size] of ASCII_code; {characters of names}
  821. @!font_check_sum:array [0..max_fonts] of integer; {check sums}
  822. @!font_scaled_size:array [0..max_fonts] of integer; {scale factors}
  823. @!font_design_size:array [0..max_fonts] of integer; {design sizes}
  824. @!font_space:array [0..max_fonts] of integer; {boundary between ``small''
  825.   and ``large'' spaces}
  826. @!font_bc:array [0..max_fonts] of integer; {beginning characters in fonts}
  827. @!font_ec:array [0..max_fonts] of integer; {ending characters in fonts}
  828. @!width_base:array [0..max_fonts] of integer; {index into |width| table}
  829. @!width:array [0..max_widths] of integer; {character widths, in \.{DVI} units}
  830. @!nf:0..max_fonts; {the number of known fonts}
  831. @!width_ptr:0..max_widths; {the number of known character widths}
  832. @ @<Set init...@>=
  833. nf:=0; width_ptr:=0; font_name[0]:=0; font_space[0]:=0;
  834. @ It is, of course, a simple matter to print the name of a given font.
  835. @p procedure print_font(@!f:integer); {|f| is an internal font number}
  836. var k:0..name_size; {index into |names|}
  837. begin if f=nf then print('UNDEFINED!')
  838. @.UNDEFINED@>
  839. else  begin for k:=font_name[f] to font_name[f+1]-1 do
  840.     print(xchr[names[k]]);
  841.   end;
  842. @ An auxiliary array |in_width| is used to hold the widths as they are
  843. input. The global variable |tfm_check_sum| is set to the check sum that
  844. appears in the current \.{TFM} file.
  845. @<Glob...@>=
  846. @!in_width:array[0..255] of integer; {\.{TFM} width data in \.{DVI} units}
  847. @!tfm_check_sum:integer; {check sum found in |tfm_file|}
  848. @ Here is a procedure that absorbs the necessary information from a
  849. \.{TFM} file, assuming that the file has just been successfully reset
  850. so that we are ready to read its first byte. (A complete description of
  851. \.{TFM} file format appears in the documentation of \.{TFtoPL} and will
  852. not be repeated here.) The procedure does not check the \.{TFM} file
  853. for validity, nor does it give explicit information about what is
  854. wrong with a \.{TFM} file that proves to be invalid; \.{DVI}-reading
  855. programs need not do this, since \.{TFM} files are almost always valid,
  856. and since the \.{TFtoPL} utility program has been specifically designed
  857. to diagnose \.{TFM} errors. The procedure simply returns |false| if it
  858. detects anything amiss in the \.{TFM} data.
  859. There is a parameter, |z|, which represents the scaling factor being
  860. used to compute the font dimensions; it must be in the range $0<z<2^{27}$.
  861. @p function in_TFM(@!z:integer):boolean; {input \.{TFM} data or return |false|}
  862. label 9997, {go here when the format is bad}
  863.   9998,  {go here when the information cannot be loaded}
  864.   9999;  {go here to exit}
  865. var k:integer; {index for loops}
  866. @!lh:integer; {length of the header data, in four-byte words}
  867. @!nw:integer; {number of words in the width table}
  868. @!wp:0..max_widths; {new value of |width_ptr| after successful input}
  869. @!alpha,@!beta:integer; {quantities used in the scaling computation}
  870. begin @<Read past the header data; |goto 9997| if there is a problem@>;
  871. @<Store character-width indices at the end of the |width| table@>;
  872. @<Read and convert the width values, setting up the |in_width| table@>;
  873. @<Move the widths from |in_width| to |width|, and append |pixel_width| values@>;
  874. width_ptr:=wp; in_TFM:=true; goto 9999;
  875. 9997: print_ln('---not loaded, TFM file is bad');
  876. @.TFM file is bad@>
  877. 9998: in_TFM:=false;
  878. 9999: end;
  879. @ @<Read past the header...@>=
  880. read_tfm_word; lh:=b2*256+b3;
  881. read_tfm_word; font_bc[nf]:=b0*256+b1; font_ec[nf]:=b2*256+b3;
  882. if font_ec[nf]<font_bc[nf] then font_bc[nf]:=font_ec[nf]+1;
  883. if width_ptr+font_ec[nf]-font_bc[nf]+1>max_widths then
  884.   begin print_ln('---not loaded, DVIDOC needs larger width table');
  885. @.DVIDOC needs larger...@>
  886.     goto 9998;
  887.   end;
  888. wp:=width_ptr+font_ec[nf]-font_bc[nf]+1;
  889. read_tfm_word; nw:=b0*256+b1;
  890. if (nw=0)or(nw>256) then goto 9997;
  891. for k:=1 to 3+lh do
  892.   begin if eof(tfm_file) then goto 9997;
  893.   read_tfm_word;
  894.   if k=4 then
  895.     if b0<128 then tfm_check_sum:=((b0*256+b1)*256+b2)*256+b3
  896.     else tfm_check_sum:=(((b0-256)*256+b1)*256+b2)*256+b3;
  897.   end;
  898. @ @<Store character-width indices...@>=
  899. if wp>0 then for k:=width_ptr to wp-1 do
  900.   begin read_tfm_word;
  901.   if b0>nw then goto 9997;
  902.   width[k]:=b0;
  903.   end;
  904. @ The most important part of |in_TFM| is the width computation, which
  905. involves multiplying the relative widths in the \.{TFM} file by the
  906. scaling factor in the \.{DVI} file. This fixed-point multiplication
  907. must be done with precisely the same accuracy by all \.{DVI}-reading programs,
  908. in order to validate the assumptions made by \.{DVI}-writing programs
  909. like \TeX82.
  910. Let us therefore summarize what needs to be done. Each width in a \.{TFM}
  911. file appears as a four-byte quantity called a |fix_word|.  A |fix_word|
  912. whose respective bytes are $(a,b,c,d)$ represents the number
  913. $$x=\left\{\vcenter{\halign{$#$,\hfil\qquad&if $#$\hfil\cr
  914. b\cdot2^{-4}+c\cdot2^{-12}+d\cdot2^{-20}&a=0;\cr
  915. -16+b\cdot2^{-4}+c\cdot2^{-12}+d\cdot2^{-20}&a=255.\cr}}\right.$$
  916. (No other choices of $a$ are allowed, since the magnitude of a \.{TFM}
  917. dimension must be less than 16.)  We want to multiply this quantity by the
  918. integer~|z|, which is known to be less than $2^{27}$. Let $\alpha=16z$.
  919. If $|z|<2^{23}$, the individual multiplications $b\cdot z$, $c\cdot z$,
  920. $d\cdot z$ cannot overflow; otherwise we will divide |z| by 2, 4, 8, or
  921. 16, to obtain a multiplier less than $2^{23}$, and we can compensate for
  922. this later. If |z| has thereby been replaced by $|z|^\prime=|z|/2^e$, let
  923. $\beta=2^{4-e}$; we shall compute
  924. $$\lfloor(b+c\cdot2^{-8}+d\cdot2^{-16})\,z^\prime/\beta\rfloor$$ if $a=0$,
  925. or the same quantity minus $\alpha$ if $a=255$.  This calculation must be
  926. done exactly, for the reasons stated above; the following program does the
  927. job in a system-independent way, assuming that arithmetic is exact on
  928. numbers less than $2^{31}$ in magnitude.
  929. @<Read and convert the width values...@>=
  930. @<Replace |z| by $|z|^\prime$ and compute $\alpha,\beta$@>;
  931. for k:=0 to nw-1 do
  932.   begin read_tfm_word;
  933.   in_width[k]:=(((((b3*z)div@'400)+(b2*z))div@'400)+(b1*z))div beta;
  934.   if b0>0 then if b0<255 then goto 9997
  935.     else in_width[k]:=in_width[k]-alpha;
  936.   end
  937. @ @<Replace |z|...@>=
  938. begin alpha:=16*z; beta:=16;
  939. while z>=@'40000000 do
  940.   begin z:=z div 2; beta:=beta div 2;
  941.   end;
  942. @ A \.{DVI}-reading program usually works with font files instead of
  943. \.{TFM} files, so \.{DVIDOC} is atypical in that respect. Font files
  944. should, however, contain exactly the same character width data that is
  945. found in the corresponding \.{TFM}s; check sums are used to help
  946. ensure this. In addition, font files usually also contain the widths of
  947. characters in pixels, since the device-independent character widths of
  948. \.{TFM} files are generally not perfect multiples of pixels.
  949. The |pixel_width| array contains this information; when |width[k]| is the
  950. device-independent width of some character in \.{DVI} units, |pixel_width[k]|
  951. is the corresponding width of that character in an actual font.
  952. The macro |char_pixel_width| is set up to be analogous to |char_width|.
  953. @d char_pixel_width(#)==pixel_width[width_base[#]+char_width_end
  954. @<Glob...@>=
  955. @!pixel_width:array[0..max_widths] of integer; {actual character widths,
  956.   in pixels}
  957. @!horiz_conv:real; {converts \.{DVI} units to horizontal pixels}
  958. @!vert_conv:real; {converts \.{DVI} units to vertical pixels}
  959. @!true_horiz_conv:real; {converts unmagnified \.{DVI} units to pixels}
  960. @!true_vert_conv:real; {converts unmagnified \.{DVI} units to pixels}
  961. @!numerator,@!denominator:integer; {stated conversion ratio}
  962. @!mag:integer; {magnification factor times 1000}
  963. @ The following code computes pixel widths by simply rounding the \.{TFM}
  964. widths to the nearest integer number of pixels, based on the conversion factor
  965. |horiz_conv| that converts \.{DVI} units to pixels. However, such a simple
  966. formula will not be valid for all fonts, and it will often give results that
  967. are off by $\pm1$ when a low-resolution font has been carefully
  968. hand-fitted. For example, a font designer often wants to make the letter `m'
  969. a pixel wider or narrower in order to make the font appear more consistent.
  970. \.{DVI}-to-printer programs should therefore input the correct pixel width
  971. information from font files whenever there is a chance that it may differ.
  972. A warning message may also be desirable in the case that at least one character
  973. is found whose pixel width differs from |conv*width| by more than a full pixel.
  974. @^system dependencies@>
  975. @d horiz_pixel_round(#)==round(horiz_conv*(#))
  976. @d vert_pixel_round(#)==round(vert_conv*(#))
  977. @<Move the widths from |in_width| to |width|, and append |pixel_width| values@>=
  978. width_base[nf]:=width_ptr-font_bc[nf];
  979. if wp>0 then for k:=width_ptr to wp-1 do
  980.   begin width[k]:=in_width[width[k]];
  981.   pixel_width[k]:=horiz_pixel_round(width[k]);
  982.   end
  983. @* Optional modes of output.
  984. \.{DVIDOC} output will vary depending on some
  985. options that the user must specify: The typeout can be confined to a
  986. restricted subset of the pages by specifying the desired starting page and
  987. the maximum number of pages.
  988. The starting page is specified by giving a sequence of 1 to 10 numbers or
  989. asterisks separated by dots. For example, the specification `\.{1.*.-5}'
  990. can be used to refer to a page output by \TeX\ when $\.{\\count0}=1$
  991. and $\.{\\count2}=-5$. (Recall that |bop| commands in a \.{DVI} file
  992. are followed by ten `count' values.) An asterisk matches any number,
  993. so the `\.*' in `\.{1.*.-5}' means that \.{\\count1} is ignored when
  994. specifying the first page. If several pages match the given specification,
  995. \.{DVIDOC} will begin with the earliest such page in the file. The
  996. default specification `\.*' (which matches all pages) therefore denotes
  997. the page at the beginning of the file.
  998. @<Glob...@>=
  999. @!max_pages:integer; {at most this many |bop..eop| pages will be printed}
  1000. @!horiz_resolution:real; {pixels per inch}
  1001. @!vert_resolution:real; {pixels per inch}
  1002. @!new_mag:integer; {if positive, overrides the postamble's magnification}
  1003. @ The starting page specification is recorded in two global arrays called
  1004. |start_count| and |start_there|. For example, `\.{1.*.-5}' is represented
  1005. by |start_there[0]=true|, |start_count[0]=1|, |start_there[1]=false|,
  1006. |start_there[2]=true|, |start_count[2]=-5|.
  1007. We also set |start_vals=2|, to indicate that count 2 was the last one
  1008. mentioned. The other values of |start_count| and |start_there| are not
  1009. important, in this example.
  1010. @<Glob...@>=
  1011. @!start_count:array[0..9] of integer; {count values to select starting page}
  1012. @!start_there:array[0..9] of boolean; {is the |start_count| value relevant?}
  1013. @!start_vals:0..9; {the last count considered significant}
  1014. @!count:array[0..9] of integer; {the count values on the current page}
  1015. @ @<Set init...@>=
  1016. max_pages:=1000000; start_vals:=0; start_there[0]:=false;
  1017. @ Here is a simple subroutine that tests if the current page might be the
  1018. starting page.
  1019. @p function start_match:boolean; {does |count| match the starting spec?}
  1020. var k:0..9;  {loop index}
  1021. @!match:boolean; {does everything match so far?}
  1022. begin match:=true;
  1023. for k:=0 to start_vals do
  1024.   if start_there[k]and(start_count[k]<>count[k]) then match:=false;
  1025. start_match:=match;
  1026. @ The |copy_ln| copies characters from the command line argument in
  1027. |cur_name|.
  1028. @^system dependencies@>
  1029. @<Glob...@>=
  1030. @!buffer:array[0..terminal_line_length] of ASCII_code;
  1031. @ The count starts at 2 to skip the -x flag characters. We subtract
  1032. 3 because |buffer| starts at 0.
  1033. @^system dependencies@>
  1034. @p procedure copy_ln; {copies a line from the terminal}
  1035. var k:0..terminal_line_length;
  1036. begin
  1037. k:=2;
  1038. while (k<name_length)and (cur_name[k] <> ' ') do begin
  1039.   buffer[k-3]:=xord[cur_name[k]]; incr(k)
  1040.   end;
  1041. buffer[k-3]:=xord[' '];
  1042. @ The global variable |buf_ptr| is used while scanning each line of input;
  1043. it points to the first unread character in |buffer|.
  1044. @<Glob...@>=
  1045. @!buf_ptr:0..terminal_line_length; {the number of characters read}
  1046. @ Here is a routine that scans a (possibly signed) integer and computes
  1047. the decimal value. If no decimal integer starts at |buf_ptr|, the
  1048. value 0 is returned. The integer should be less than $2^{31}$ in
  1049. absolute value.
  1050. @p function get_integer:integer;
  1051. var x:integer; {accumulates the value}
  1052. @!negative:boolean; {should the value be negated?}
  1053. begin if buffer[buf_ptr]="-" then
  1054.   begin negative:=true; incr(buf_ptr);
  1055.   end
  1056. else negative:=false;
  1057. x:=0;
  1058. while (buffer[buf_ptr]>="0")and(buffer[buf_ptr]<="9") do
  1059.   begin x:=10*x+buffer[buf_ptr]-"0"; incr(buf_ptr);
  1060.   end;
  1061. if negative then get_integer:=-x @+ else get_integer:=x;
  1062. @ The selected options are put into global variables by the |options|
  1063. procedure, which is called just as \.{DVIDOC} begins.
  1064. @^system dependencies@>
  1065. @p procedure options;
  1066. var j,k:integer; {loop variable}
  1067. begin
  1068. log_errors:=false;
  1069. start_vals:=0; start_there[0]:=false;
  1070. max_pages:=1000000;
  1071. horiz_resolution:=13.76582;    {cmtt10 chars per inch}
  1072. vert_resolution:=6.0225;    {72.27pt/12pt}
  1073. new_mag:=0;
  1074. base_name[1] := ' ';
  1075. j := 1;
  1076. while j < argc do begin
  1077.     argv(j, cur_name);
  1078.     if cur_name[1] = '-' then begin
  1079.     if cur_name[2] = 'e' then log_errors := true
  1080.     else if cur_name[2] = 's' then begin
  1081.         @<Determine the desired |start_count| values@>
  1082.         end
  1083.     else if cur_name[2] = 'm' then begin
  1084.         @<Determine the desired |max_pages|@>
  1085.         end
  1086.     else
  1087.         cannot_start('Usage: dvidoc [-e] [-s<start page spec>] [-m<maxpages>] <dvi-file>')
  1088.     else if base_name[1] = ' ' then
  1089.     argv(j, base_name)
  1090.     else
  1091.     cannot_start('Only one input file please');
  1092.     incr(j)
  1093.     end;
  1094. if base_name[1] = ' ' then
  1095.     cannot_start('No input file specified');
  1096. @ @<Determine the desired |start...@>=
  1097. copy_ln; buf_ptr:=0; k:=0;
  1098. if buffer[0]<>" " then
  1099.   repeat if buffer[buf_ptr]="*" then
  1100.     begin start_there[k]:=false; incr(buf_ptr);
  1101.     end
  1102.   else  begin start_there[k]:=true; start_count[k]:=get_integer;
  1103.     end;
  1104.   if (k<9)and(buffer[buf_ptr]=".") then
  1105.     begin incr(k); incr(buf_ptr);
  1106.     end
  1107.   else if buffer[buf_ptr]=" " then start_vals:=k
  1108.   else
  1109.     cannot_start('1.*.-5 specifies first page with \count0=1, \count2=-5.')
  1110.   until start_vals=k
  1111. @ @<Determine the desired |max_pages|@>=
  1112. copy_ln; buf_ptr:=0;
  1113. if buffer[0]<>" " then
  1114.   begin max_pages:=get_integer;
  1115.   if max_pages<=0 then
  1116.     cannot_start('Maxpages should be a positive number.')
  1117.   end
  1118. @* Defining fonts.
  1119. \.{DVIDOC} reads the postamble first and loads
  1120. all of the fonts defined there; then it processes the pages. In this
  1121. case, a \\{fnt\_def} command should match a previous definition if and only
  1122. if the \\{fnt\_def} being processed is not in the postamble. 
  1123. A global variable |in_postamble| is provided to tell whether we are
  1124. processing the postamble or not.
  1125. @<Glob...@>=
  1126. @!in_postamble:boolean; {are we reading the postamble?}
  1127. @ @<Set init...@>=
  1128. in_postamble:=false;
  1129. @ The following subroutine does the necessary things when a \\{fnt\_def}
  1130. command is being processed.
  1131. @p procedure define_font(@!e:integer); {|e| is an external font number}
  1132. var f:0..max_fonts;
  1133. @!p:integer; {length of the area/directory spec}
  1134. @!n:integer; {length of the font name proper}
  1135. @!c,@!q,@!d:integer; {check sum, scaled size, and design size}
  1136. @!r:0..name_length; {index into |cur_name|}
  1137. @!j,@!k:0..name_size; {indices into |names|}
  1138. @!mismatch:boolean; {do names disagree?}
  1139. begin if nf=max_fonts then abort('DVIDOC capacity exceeded (max fonts=',
  1140.     max_fonts:1,')!');
  1141. @.DVIDOC capacity exceeded...@>
  1142. font_num[nf]:=e; f:=0;
  1143. while font_num[f]<>e do incr(f);
  1144. @<Read the font parameters into position for font |nf|, and
  1145.   print the font name@>;
  1146. if in_postamble then
  1147.   begin if f<nf then print_ln('---this font was already defined!');
  1148. @.this font was already defined@>
  1149.   end
  1150. else  begin if f=nf then print_ln('---this font wasn''t loaded before!');
  1151. @.this font wasn't loaded before@>
  1152.   end;
  1153. if f=nf then @<Load the new font, unless there are problems@>
  1154. else @<Check that the current font definition matches the old one@>;
  1155. @ @<Check that the current...@>=
  1156. begin if font_check_sum[f]<>c then
  1157.   print_ln('---check sum doesn''t match previous definition!');
  1158. @.check sum doesn't match@>
  1159. if font_scaled_size[f]<>q then
  1160.   print_ln('---scaled size doesn''t match previous definition!');
  1161. @.scaled size doesn't match@>
  1162. if font_design_size[f]<>d then
  1163.   print_ln('---design size doesn''t match previous definition!');
  1164. @.design size doesn't match@>
  1165. j:=font_name[f]; k:=font_name[nf]; mismatch:=false;
  1166. while j<font_name[f+1] do
  1167.   begin if names[j]<>names[k] then mismatch:=true;
  1168.   incr(j); incr(k);
  1169.   end;
  1170. if k<>font_name[nf+1] then mismatch:=true;
  1171. if mismatch then print_ln('---font name doesn''t match previous definition!');
  1172. @.font name doesn't match@>
  1173. print_ln(' ')
  1174. @ @<Read the font parameters into position for font |nf|...@>=
  1175. c:=signed_quad; font_check_sum[nf]:=c;@/
  1176. q:=signed_quad; font_scaled_size[nf]:=q;@/
  1177. d:=signed_quad; font_design_size[nf]:=d;@/
  1178. p:=get_byte; n:=get_byte;
  1179. if font_name[nf]+n+p>name_size then
  1180.   abort('DVIDOC capacity exceeded (name size=',name_size:1,')!');
  1181. @.DVIDOC capacity exceeded...@>
  1182. font_name[nf+1]:=font_name[nf]+n+p;
  1183. print('Font ',e:1,': ');
  1184. if n+p=0 then print('null font name!')
  1185. @.null font name@>
  1186. else for k:=font_name[nf] to font_name[nf+1]-1 do names[k]:=get_byte;
  1187. incr(nf); print_font(nf-1); decr(nf)
  1188. @ @<Load the new font, unless there are problems@>=
  1189. begin @<Move font name into the |cur_name| string@>;
  1190. open_tfm_file;
  1191. if eof(tfm_file) then
  1192.   print('---not loaded, TFM file can''t be opened!')
  1193. @.TFM file can\'t be opened@>
  1194. else  begin if (q<=0)or(q>=@'1000000000) then
  1195.     print('---not loaded, bad scale (',q:1,')!')
  1196. @.bad scale@>
  1197.   else if (d<=0)or(d>=@'1000000000) then
  1198.     print('---not loaded, bad design size (',d:1,')!')
  1199. @.bad design size@>
  1200.   else if in_TFM(q) then @<Finish loading the new font info@>;
  1201.   end;
  1202. print_ln(' ');
  1203. @ @<Finish loading...@>=
  1204. begin font_space[nf]:=q div 6; {this is a 3-unit ``thin space''}
  1205. if (c<>0)and(tfm_check_sum<>0)and(c<>tfm_check_sum) then
  1206.   begin print_ln('---beware: check sums do not agree!');
  1207. @.beware: check sums do not agree@>
  1208. @.check sums do not agree@>
  1209.   print_ln('   (',c:1,' vs. ',tfm_check_sum:1,')');
  1210.   print('   ');
  1211.   end;
  1212. print('---loaded at size ',q:1,' DVI units');
  1213. d:=round((100.0*horiz_conv*q)/(true_horiz_conv*d));
  1214. if d<>100 then
  1215.   begin print_ln(' '); print(' (this font is magnified ',d:1,'%)');
  1216.   end;
  1217. @.this font is magnified@>
  1218. incr(nf); {now the new font is officially present}
  1219. font_space[nf]:=0; {for |out_space| and |out_vmove|}
  1220. @ If |p=0|, i.e., if no font directory has been specified, \.{DVIDOC}
  1221. is supposed to use the default font directory, which is a
  1222. system-dependent place where the standard fonts are kept.
  1223. The string variable |default_directory| contains the name of this area.
  1224. @^system dependencies@>
  1225. @d default_directory_name=='TeXfonts:' {change this to the correct name}
  1226. @d default_directory_name_length=9 {change this to the correct length}
  1227. @<Glob...@>=
  1228. @!default_directory:packed array[1..default_directory_name_length] of char;
  1229. @ @<Set init...@>=
  1230. default_directory:=default_directory_name;
  1231. @ The string |cur_name| is supposed to be set to the external name of the
  1232. \.{TFM} file for the current font. This usually means that we need to,
  1233. at most sites, append the
  1234. suffix ``.tfm''.
  1235. @^system dependencies@>
  1236. @<Move font name into the |cur_name| string@>=
  1237. for k:=1 to name_length do cur_name[k]:=' ';
  1238. r:=0;
  1239. for k:=font_name[nf] to font_name[nf+1]-1 do
  1240.   begin incr(r);
  1241.   if r+4>name_length then
  1242.     abort('DVIDOC capacity exceeded (max font name length=',
  1243.       name_length:1,')!');
  1244. @.DVIDOC capacity exceeded...@>
  1245.     cur_name[r]:=xchr[names[k]];
  1246.     end;
  1247. cur_name[r+1]:='.'; cur_name[r+2]:='t'; cur_name[r+3]:='f'; cur_name[r+4]:='m'
  1248. @* Low level output routines.
  1249. Characters set by the \.{DVI} file are placed in |page_buffer|, a two
  1250. dimensional array of characters with one element for each print
  1251. position on the page.  The |page_buffer| is cleared at the beginning
  1252. of each page and printed at the end of each page.  
  1253. |doc_file|, the file to which the document is destined, is an ordinary text
  1254. file.
  1255. To optimize the initialization and printing of |page_buffer|, a high
  1256. water mark line number, |page_hwm|, is kept to indicate the last line
  1257. that contains any printable characters, and for each line a high water
  1258. mark character number, |line_hwm|, is kept to indicate the location of
  1259. the last printable character in the line.
  1260. @<Glob...@>=
  1261. @!i:1..name_length;
  1262. @!log_errors:boolean;
  1263. @!err_file:text_file;
  1264. @!page_buffer:packed array[1..page_width_max,1..page_length_max] of ASCII_code;
  1265.                  {storage for a document page}
  1266. @!line_hwm:array[1..page_length_max] of 0..page_width_max;
  1267.                  {high water marks for each line}
  1268. @!page_hwm: 0..page_length_max;  {high water mark for page}
  1269. @ |err_file| needs to be opened. Create |err_file| with a suffix of .err.
  1270. @<Create error message file@>=
  1271. if log_errors then begin
  1272.     cur_name := base_name;
  1273.     i := 1;
  1274.     while (i <= name_length) and (cur_name[i] <> ' ') do
  1275.     incr(i);
  1276.     cur_name[i-3] := 'e'; cur_name[i-2] := 'r'; cur_name[i-1] := 'r';
  1277.     if test_access(write_access_mode, no_file_path) then
  1278.     rewrite(err_file, cur_name)
  1279.     else
  1280.     cannot_start('Cannot create error file')
  1281.     end
  1282. else begin
  1283.     cur_name := '/dev/null';
  1284.     if test_access(write_access_mode, no_file_path) then
  1285.     rewrite(err_file, cur_name)
  1286.     else
  1287.     cannot_start('Somebody stole /dev/null!')
  1288.     end;
  1289. @ The |flush_page| procedure will print the |page_buffer|.
  1290. @p procedure flush_page;
  1291. var i:0..page_width_max; j:0..page_length_max;
  1292. begin
  1293.   for j := 1 to page_hwm do begin
  1294.     for i := 1 to line_hwm[j] do
  1295.       write (doc_file, xchr[page_buffer[i,j]]);
  1296.     write_ln (doc_file) end;
  1297.   write (doc_file, chr(12))  {end the page with a form feed}  
  1298. @ The |empty_page| procedure will empty the |page_buffer| data structure.
  1299. @p procedure empty_page;
  1300. begin page_hwm := 0 end;
  1301. @ And the |out_char| procedure puts something into it.  The usual printable
  1302. ASCII characters will be put into the buffer as is.  Non-printable characters,
  1303. including the blank, will be put into the buffer as question mark chracters.
  1304. @p procedure out_char(p,hh,vv:integer);
  1305. var i:1..page_width_max; j:1..page_length_max;
  1306.      {|hh| and |vv| range from zero up while |i| and |j| range from one up.}
  1307.     k: integer;
  1308.     c: ASCII_code;
  1309. begin
  1310.   if 
  1311.     (p>" ")and(p<="~") then c:=p
  1312.     else c:=xord['?'];
  1313.   if 
  1314.     (hh>page_width_max-1) or (vv>page_length_max-1) then begin 
  1315.       print_ln(' ');
  1316.       print('Character "', xchr[c], '" set at column ', hh+1:1);
  1317.       print_ln( ' and row ', vv+1:1, ',');
  1318.       print('outside the range of DVIDOC (');
  1319. @.outside the range of DVIDOC@>
  1320.       print(page_width_max:1, ',', page_length_max:1, ').');
  1321.       print_ln(' ') end
  1322.     else begin
  1323.       i := hh + 1;
  1324.       j := vv + 1;
  1325.       if j>page_hwm then begin {initialize any as yet untouched lines}
  1326.         for k := page_hwm+1 to j do line_hwm[k]:=0;
  1327.         page_hwm := j end;
  1328.       if i>line_hwm[j] then begin {initialize any as yet untouched characters}
  1329.         for k := line_hwm[j]+1 to i do page_buffer[k,j] := xord[' '];
  1330.         line_hwm[j] := i end;
  1331.       page_buffer[i,j] := c {put the character in its place}  end
  1332. @* Translation to symbolic form.
  1333. The main work of \.{DVIDOC} is accomplished by the |do_page| procedure,
  1334. which produces the output for an entire page, assuming that the |bop|
  1335. command for that page has already been processed. This procedure is
  1336. essentially an interpretive routine that reads and acts on the \.{DVI}
  1337. commands.
  1338. @ The definition of \.{DVI} files refers to six registers,
  1339. $(h,v,w,x,y,z)$, which hold integer values in \.{DVI} units.  In practice,
  1340. we also need registers |hh| and |vv|, the pixel analogs of $h$ and $v$,
  1341. since it is not always true that |hh=horiz_pixel_round(h)| or
  1342. |vv=vert_pixel_round(v)|.
  1343. The stack of $(h,v,w,x,y,z)$ values is represented by eight arrays
  1344. called |hstack|, \dots, |zstack|, |hhstack|, and |vvstack|.
  1345. @<Glob...@>=
  1346. @!h,@!v,@!w,@!x,@!y,@!z,@!hh,@!vv:integer; {current state values}
  1347. @!hstack,@!vstack,@!wstack,@!xstack,@!ystack,@!zstack:
  1348.   array [0..stack_size] of integer; {pushed down values in \.{DVI} units}
  1349. @!hhstack,@!vvstack:
  1350.   array [0..stack_size] of integer; {pushed down values in pixels}
  1351. @ Three characteristics of the pages (their |max_v|, |max_h|, and
  1352. |max_s|) are specified in the postamble, and a warning message
  1353. is printed if these limits are exceeded. Actually |max_v| is set to
  1354. the maximum height plus depth of a page, and |max_h| to the maximum width,
  1355. for purposes of page layout. Since characters can legally be set outside
  1356. of the page boundaries, it is not an error when |max_v| or |max_h| is
  1357. exceeded. But |max_s| should not be exceeded.
  1358. The postamble also specifies the total number of pages; \.{DVIDOC}
  1359. checks to see if this total is accurate.
  1360. @<Glob...@>=
  1361. @!max_v:integer; {the value of |abs(v)| should probably not exceed this}
  1362. @!max_h:integer; {the value of |abs(h)| should probably not exceed this}
  1363. @!max_s:integer; {the stack depth should not exceed this}
  1364. @!max_v_so_far,@!max_h_so_far,@!max_s_so_far:integer; {the record high levels}
  1365. @!total_pages:integer; {the stated total number of pages}
  1366. @!page_count:integer; {the total number of pages seen so far}
  1367. @ @<Set init...@>=
  1368. max_v:=@'17777777777-99; max_h:=@'17777777777-99; max_s:=stack_size+1;@/
  1369. max_v_so_far:=0; max_h_so_far:=0; max_s_so_far:=0; page_count:=0;
  1370. @ Before we get into the details of |do_page|, it is convenient to
  1371. consider a simpler routine that computes the first parameter of each
  1372. opcode.
  1373. @d four_cases(#)==#,#+1,#+2,#+3
  1374. @d eight_cases(#)==four_cases(#),four_cases(#+4)
  1375. @d sixteen_cases(#)==eight_cases(#),eight_cases(#+8)
  1376. @d thirty_two_cases(#)==sixteen_cases(#),sixteen_cases(#+16)
  1377. @d sixty_four_cases(#)==thirty_two_cases(#),thirty_two_cases(#+32)
  1378. @p function first_par(o:eight_bits):integer;
  1379. begin case o of
  1380. sixty_four_cases(set_char_0),sixty_four_cases(set_char_0+64):
  1381.   first_par:=o-set_char_0;
  1382. set1,put1,fnt1,xxx1,fnt_def1: first_par:=get_byte;
  1383. set1+1,put1+1,fnt1+1,xxx1+1,fnt_def1+1: first_par:=get_two_bytes;
  1384. set1+2,put1+2,fnt1+2,xxx1+2,fnt_def1+2: first_par:=get_three_bytes;
  1385. right1,w1,x1,down1,y1,z1: first_par:=signed_byte;
  1386. right1+1,w1+1,x1+1,down1+1,y1+1,z1+1: first_par:=signed_pair;
  1387. right1+2,w1+2,x1+2,down1+2,y1+2,z1+2: first_par:=signed_trio;
  1388. set1+3,set_rule,put1+3,put_rule,right1+3,w1+3,x1+3,down1+3,y1+3,z1+3,
  1389.   fnt1+3,xxx1+3,fnt_def1+3: first_par:=signed_quad;
  1390. nop,bop,eop,push,pop,pre,post,post_post,undefined_commands: first_par:=0;
  1391. w0: first_par:=w;
  1392. x0: first_par:=x;
  1393. y0: first_par:=y;
  1394. z0: first_par:=z;
  1395. sixty_four_cases(fnt_num_0): first_par:=o-fnt_num_0;
  1396. @ Here are two other subroutines that we need: They compute the number of
  1397. pixels in the height or width of a rule. Characters and rules will line up
  1398. properly if the sizes are computed precisely as specified here.  (Since
  1399. |horiz_conv| and |vert_conv| 
  1400. are computed with some floating-point roundoff error, in a
  1401. machine-dependent way, format designers who are tailoring something for a
  1402. particular resolution should not plan their measurements to come out to an
  1403. exact integer number of pixels; they should compute things so that the
  1404. rule dimensions are a little less than an integer number of pixels, e.g.,
  1405. 4.99 instead of 5.00.)
  1406. @p function horiz_rule_pixels(x:integer):integer;
  1407.   {computes $\lceil|horiz_conv|\cdot x\rceil$}
  1408. var n:integer;
  1409. begin n:=trunc(horiz_conv*x);
  1410. if n<horiz_conv*x then horiz_rule_pixels:=n+1 @+ else horiz_rule_pixels:=n;
  1411. function vert_rule_pixels(x:integer):integer;
  1412.   {computes $\lceil|vert_conv|\cdot x\rceil$}
  1413. var n:integer;
  1414. begin n:=trunc(vert_conv*x);
  1415. if n<vert_conv*x then vert_rule_pixels:=n+1 @+ else vert_rule_pixels:=n;
  1416. @ Strictly speaking, the |do_page| procedure is really a function with
  1417. side effects, not a `\&{procedure}'\thinspace; it returns the value |false|
  1418. if \.{DVIDOC} should be aborted because of some unusual happening. The
  1419. subroutine is organized as a typical interpreter, with a multiway branch
  1420. on the command code followed by |goto| statements leading to routines that
  1421. finish up the activities common to different commands. We will use the
  1422. following labels:
  1423. @d fin_set=41 {label for commands that set or put a character}
  1424. @d fin_rule=42 {label for commands that set or put a rule}
  1425. @d move_right=43 {label for commands that change |h|}
  1426. @d move_down=44 {label for commands that change |v|}
  1427. @d show_state=45 {label for commands that change |s|}
  1428. @d change_font=46 {label for commands that change |cur_font|}
  1429. @ Some \PASCAL\ compilers severely restrict the length of procedure bodies,
  1430. so we shall split |do_page| into two parts, one of which is
  1431. called |special_cases|. The different parts communicate with each other
  1432. via the global variables mentioned above, together with the following ones:
  1433. @<Glob...@>=
  1434. @!s:integer; {current stack size}
  1435. @!ss:integer; {stack size to print}
  1436. @!cur_font:integer; {current internal font number}
  1437. @ Here is the overall setup.
  1438. @p @t\4@>@<Declare the function called |special_cases|@>@;
  1439. function do_page:boolean;
  1440. label fin_set,fin_rule,move_right,show_state,done,9998,9999;
  1441. var o:eight_bits; {operation code of the current command}
  1442. @!p,@!q:integer; {parameters of the current command}
  1443. @!a:integer; {byte number of the current command}
  1444. i,j:integer; {for loop indices for setting rules}
  1445. @!hhh:integer; {|h|, rounded to the nearest pixel}
  1446. begin empty_page; cur_font:=nf; {set current font undefined}
  1447. s:=0; h:=0; v:=0; w:=0; x:=0; y:=0; z:=0; hh:=0; vv:=0;
  1448.   {initialize the state variables}
  1449. while true do @<Translate the next command in the \.{DVI} file;
  1450.     |goto 9999| with |do_page=true| if it was |eop|;
  1451.     |goto 9998| if premature termination is needed@>;
  1452. 9998: print_ln('!'); do_page:=false;
  1453. 9999: end;
  1454. @<Translate the next command...@>=
  1455. begin a:=cur_loc; 
  1456. o:=get_byte; p:=first_par(o);
  1457. if eof(dvi_file) then bad_dvi('the file ended prematurely');
  1458. @.the file ended prematurely@>
  1459. @<Start translation of command |o| and |goto| the appropriate label to
  1460.   finish the job@>;
  1461. fin_set: @<Finish a command that either sets or puts a character, then
  1462.     |goto move_right| or |done|@>;
  1463. fin_rule: @<Finish a command that either sets or puts a rule, then
  1464.     |goto move_right| or |done|@>;
  1465. move_right: @<Finish a command that sets |h:=h+q|, then |goto done|@>;
  1466. show_state: ;
  1467. done: ;
  1468. @ The multiway switch in |first_par|, above, was organized by the length
  1469. of each command; the one in |do_page| is organized by the semantics.
  1470. @<Start translation...@>=
  1471. if o<set_char_0+128 then @<Translate a |set_char| command@>
  1472. else case o of
  1473.   four_cases(set1): begin out_char(p,hh,vv); goto fin_set;
  1474.     end;
  1475.   set_rule: begin goto fin_rule;
  1476.     end;
  1477.   put_rule: begin goto fin_rule;
  1478.     end;
  1479.   @t\4@>@<Cases for commands |nop|, |bop|, \dots, |pop|@>@;
  1480.   @t\4@>@<Cases for horizontal motion@>@;
  1481.   othercases if special_cases(o,p,a) then goto done@+else goto 9998
  1482.   endcases
  1483. @ @<Declare the function called |special_cases|@>=
  1484. function special_cases(@!o:eight_bits;@!p,@!a:integer):boolean;
  1485. label change_font,move_down,done,9998;
  1486. var q:integer; {parameter of the current command}
  1487. @!k:integer; {loop index}
  1488. @!bad_char:boolean; {has a non-ASCII character code appeared in this \\{xxx}?}
  1489. @!pure:boolean; {is the command error-free?}
  1490. @!vvv:integer; {|v|, rounded to the nearest pixel}
  1491. begin pure:=true;
  1492. case o of
  1493. four_cases(put1): begin goto done;
  1494.   end;
  1495. @t\4@>@<Cases for vertical motion@>@;
  1496. @t\4@>@<Cases for fonts@>@;
  1497. four_cases(xxx1): @<Translate an |xxx| command and |goto done|@>;
  1498. pre: begin error('preamble command within a page!'); goto 9998;
  1499.   end;
  1500. @.preamble command within a page@>
  1501. post,post_post: begin error('postamble command within a page!'); goto 9998;
  1502. @.postamble command within a page@>
  1503.   end;
  1504. othercases begin error('undefined command ',o:1,'!');
  1505.   goto done;
  1506. @.undefined command@>
  1507.   end
  1508. endcases;
  1509. move_down: @<Finish a command that sets |v:=v+p|, then |goto done|@>;
  1510. change_font: @<Finish a command that changes the current font,
  1511.   then |goto done|@>;
  1512. 9998: pure:=false;
  1513. done: special_cases:=pure;
  1514. @ @<Cases for commands |nop|, |bop|, \dots, |pop|@>=
  1515. nop: begin goto done;
  1516.   end;
  1517. bop: begin error('bop occurred before eop!'); goto 9998;
  1518. @.bop occurred before eop@>
  1519.   end;
  1520. eop: begin 
  1521.   if s<>0 then error('stack not empty at end of page (level ',
  1522.     s:1,')!');
  1523. @.stack not empty...@>
  1524.   do_page:=true; flush_page; goto 9999;
  1525.   end;
  1526. push: begin 
  1527.   if s=max_s_so_far then
  1528.     begin max_s_so_far:=s+1;
  1529.     if s=max_s then error('deeper than claimed in postamble!');
  1530. @.deeper than claimed...@>
  1531. @.push deeper than claimed...@>
  1532.     if s=stack_size then
  1533.       begin error('DVIDOC capacity exceeded (stack size=',
  1534.         stack_size:1,')'); goto 9998;
  1535.       end;
  1536.     end;
  1537.   hstack[s]:=h; vstack[s]:=v; wstack[s]:=w;
  1538.   xstack[s]:=x; ystack[s]:=y; zstack[s]:=z;
  1539.   hhstack[s]:=hh; vvstack[s]:=vv; incr(s); ss:=s-1; goto show_state;
  1540.   end;
  1541. pop: begin 
  1542.   if s=0 then error('Pop illegal at level zero!')
  1543.   else  begin decr(s); hh:=hhstack[s]; vv:=vvstack[s];
  1544.     h:=hstack[s]; v:=vstack[s]; w:=wstack[s];
  1545.     x:=xstack[s]; y:=ystack[s]; z:=zstack[s];
  1546.     end;
  1547.   ss:=s; goto show_state;
  1548.   end;
  1549. @ Rounding to the nearest pixel is best done in the manner shown here, so as
  1550. to be inoffensive to the eye: When the horizontal motion is small, like a
  1551. kern, |hh| changes by rounding the kern; but when the motion is large, |hh|
  1552. changes by rounding the true position |h| so that accumulated rounding errors
  1553. disappear.
  1554. @d out_space==if abs(p)>=font_space[cur_font] then
  1555.     begin hh:=horiz_pixel_round(h+p);
  1556.     end
  1557.   else hh:=hh+horiz_pixel_round(p);
  1558.   q:=p; goto move_right
  1559. @<Cases for horizontal motion@>=
  1560. four_cases(right1):begin out_space;
  1561.   end;
  1562. w0,four_cases(w1):begin w:=p; out_space;
  1563.   end;
  1564. x0,four_cases(x1):begin x:=p; out_space;
  1565.   end;
  1566. @ Vertical motion is done similarly, but with the threshold between
  1567. ``small'' and ``large'' increased by a factor of five. The idea is to make
  1568. fractions like ``$1\over2$'' round consistently, but to absorb accumulated
  1569. rounding errors in the baseline-skip moves.
  1570. @d out_vmove==if abs(p)>=5*font_space[cur_font] then vv:=vert_pixel_round(v+p)
  1571.   else vv:=vv+vert_pixel_round(p);
  1572.   goto move_down
  1573. @<Cases for vertical motion@>=
  1574. four_cases(down1):begin out_vmove;
  1575.   end;
  1576. y0,four_cases(y1):begin y:=p; out_vmove;
  1577.   end;
  1578. z0,four_cases(z1):begin z:=p; out_vmove;
  1579.   end;
  1580. @ @<Cases for fonts@>=
  1581. sixty_four_cases(fnt_num_0): begin 
  1582.   goto change_font;
  1583.   end;
  1584. four_cases(fnt1): begin 
  1585.   goto change_font;
  1586.   end;
  1587. four_cases(fnt_def1): begin 
  1588.   define_font(p); goto done;
  1589.   end;
  1590. @ @<Translate an |xxx| command and |goto done|@>=
  1591. begin print('xxx'''); bad_char:=false;
  1592. for k:=1 to p do
  1593.   begin q:=get_byte;
  1594.   if (q<" ")or(q>"~") then bad_char:=true;
  1595.   print(xchr[q]);
  1596.   end;
  1597. print('''');
  1598. if bad_char then error('non-ASCII character in xxx command!');
  1599. @.non-ASCII character...@>
  1600. goto done;
  1601. @ @<Translate a |set_char|...@>=
  1602. begin 
  1603.       out_char(p,hh,vv)  
  1604. @ @<Finish a command that either sets or puts a character...@>=
  1605. if p<0 then p:=255-((-1-p) mod 256)
  1606. else if p>=256 then p:=p mod 256; {width computation for oriental fonts}
  1607. @^oriental characters@>@^Chinese characters@>@^Japanese characters@>
  1608. if (p<font_bc[cur_font])or(p>font_ec[cur_font]) then q:=invalid_width
  1609. else q:=char_width(cur_font)(p);
  1610. if q=invalid_width then
  1611.   begin error('character ',p:1,' invalid in font ');
  1612. @.character $c$ invalid...@>
  1613.   print_font(cur_font);
  1614.   if cur_font<>nf then print('!'); {font |nf| has `\.!' in its name}
  1615.   end;
  1616. if o>=put1 then goto done;
  1617. if q=invalid_width then q:=0
  1618. else hh:=hh+char_pixel_width(cur_font)(p);
  1619. goto move_right
  1620. @ @<Finish a command that either sets or puts a rule...@>=
  1621. q:=signed_quad;
  1622. if (p>0) and (q>0) then
  1623.   for i:=hh to hh+horiz_rule_pixels(q)-1 do
  1624.     for j:=vv downto vv-vert_rule_pixels(p)+1 do
  1625.       out_char(xord['-'],i,j);
  1626. if o=put_rule then goto done;
  1627. hh:=hh+horiz_rule_pixels(q); goto move_right
  1628. @ Since \.{DVIDOC} is intended to diagnose strange errors, it checks
  1629. carefully to make sure that |h| and |v| do not get out of range.
  1630. Normal \.{DVI}-reading programs need not do this.
  1631. @d infinity==@'17777777777 {$\infty$ (approximately)}
  1632. @d max_drift=2 {we insist that abs|(hh-pixel_round(h))<=max_drift|}
  1633. @<Finish a command that sets |h:=h+q|, then |goto done|@>=
  1634. if (h>0)and(q>0) then if h>infinity-q then
  1635.   begin error('arithmetic overflow! parameter changed from ',
  1636. @.arithmetic overflow...@>
  1637.     q:1,' to ',infinity-h:1);
  1638.   q:=infinity-h;
  1639.   end;
  1640. if (h<0)and(q<0) then if -h>q+infinity then
  1641.   begin error('arithmetic overflow! parameter changed from ',
  1642.     q:1, ' to ',(-h)-infinity:1);
  1643.   q:=(-h)-infinity;
  1644.   end;
  1645. hhh:=horiz_pixel_round(h+q);
  1646. if abs(hhh-hh)>max_drift then
  1647.   if hhh>hh then hh:=hhh-max_drift
  1648.   else hh:=hhh+max_drift;
  1649. h:=h+q;
  1650. if abs(h)>max_h_so_far then
  1651.   begin if abs(h)>max_h+99 then
  1652.     begin error('warning: |h|>',max_h:1,'!');
  1653. @.warning: |h|...@>
  1654.     max_h:=abs(h);
  1655.     end;
  1656.   max_h_so_far:=abs(h);
  1657.   end;
  1658. goto done
  1659. @ @<Finish a command that sets |v:=v+p|, then |goto done|@>=
  1660. if (v>0)and(p>0) then if v>infinity-p then
  1661.   begin error('arithmetic overflow! parameter changed from ',
  1662. @.arithmetic overflow...@>
  1663.     p:1,' to ',infinity-v:1);
  1664.   p:=infinity-v;
  1665.   end;
  1666. if (v<0)and(p<0) then if -v>p+infinity then
  1667.   begin error('arithmetic overflow! parameter changed from ',
  1668.     p:1, ' to ',(-v)-infinity:1);
  1669.   p:=(-v)-infinity;
  1670.   end;
  1671. vvv:=vert_pixel_round(v+p);
  1672. if abs(vvv-vv)>max_drift then
  1673.   if vvv>vv then vv:=vvv-max_drift
  1674.   else vv:=vvv+max_drift;
  1675. v:=v+p;
  1676. if abs(v)>max_v_so_far then
  1677.   begin if abs(v)>max_v+99 then
  1678.     begin error('warning: |v|>',max_v:1,'!');
  1679. @.warning: |v|...@>
  1680.     max_v:=abs(v);
  1681.     end;
  1682.   max_v_so_far:=abs(v);
  1683.   end;
  1684. goto done
  1685. @ @<Finish a command that changes the current font...@>=
  1686. font_num[nf]:=p; cur_font:=0;
  1687. while font_num[cur_font]<>p do incr(cur_font);
  1688. goto done
  1689. @* Skipping pages.
  1690. @ Global variables called |old_backpointer| and |new_backpointer|
  1691. are used to check whether the back pointers are properly set up.
  1692. Another one tells whether we have already found the starting page.
  1693. @<Glob...@>=
  1694. @!old_backpointer:integer; {the previous |bop| command location}
  1695. @!new_backpointer:integer; {the current |bop| command location}
  1696. @!started:boolean; {has the starting page been found?}
  1697. @ @<Set init...@>=
  1698. old_backpointer:=-1; started:=false;
  1699. @ @<Pass a |bop| command, setting up the |count| array@>=
  1700. new_backpointer:=cur_loc-1; incr(page_count);
  1701. for k:=0 to 9 do count[k]:=signed_quad;
  1702. if signed_quad<>old_backpointer
  1703.   then print_ln('backpointer in byte ',cur_loc-4:1,
  1704.     ' should be ',old_backpointer:1,'!');
  1705. @.backpointer...should be p@>
  1706. old_backpointer:=new_backpointer
  1707. @* Using the backpointers.
  1708. First comes a routine that illustrates how to find the postamble quickly.
  1709. @<Find the postamble, working back from the end@>=
  1710. n:=dvi_length;
  1711. if n<53 then bad_dvi('only ',n:1,' bytes long');
  1712. @.only n bytes long@>
  1713. m:=n-4;
  1714. repeat if m=0 then bad_dvi('all 223s');
  1715. @.all 223s@>
  1716. move_to_byte(m); k:=get_byte; decr(m);
  1717. until k<>223;
  1718. if k<>id_byte then bad_dvi('ID byte is ',k:1);
  1719. @.ID byte is wrong@>
  1720. move_to_byte(m-3); q:=signed_quad;
  1721. if (q<0)or(q>m-33) then bad_dvi('post pointer ',q:1,' at byte ',m-3:1);
  1722. @.post pointer is wrong@>
  1723. move_to_byte(q); k:=get_byte;
  1724. if k<>post then bad_dvi('byte ',q:1,' is not post');
  1725. @.byte n is not post@>
  1726. post_loc:=q; first_backpointer:=signed_quad
  1727. @ Note that the last steps of the above code save the locations of the
  1728. the |post| byte and the final |bop|.  We had better declare these global
  1729. variables, together with another one that we will need shortly.
  1730. @<Glob...@>=
  1731. @!post_loc:integer; {byte location where the postamble begins}
  1732. @!first_backpointer:integer; {the pointer following |post|}
  1733. @!start_loc:integer; {byte location of the first page to process}
  1734. @ The next little routine shows how the backpointers can be followed
  1735. to move through a \.{DVI} file in reverse order. Ordinarily a \.{DVI}-reading
  1736. program would do this only if it wants to print the pages backwards or
  1737. if it wants to find a specified starting page that is not necessarily the
  1738. first page in the file; otherwise it would of course be simpler and faster
  1739. just to read the whole file from the beginning.
  1740. @<Count the pages and move to the starting page@>=
  1741. q:=post_loc; p:=first_backpointer; start_loc:=-1;
  1742. if p>=0 then
  1743.   repeat {now |q| points to a |post| or |bop| command; |p>=0| is prev pointer}
  1744.   if p>q-46 then
  1745.     bad_dvi('page link ',p:1,' after byte ',q:1);
  1746. @.page link wrong...@>
  1747.   q:=p; move_to_byte(q); k:=get_byte;
  1748.   if k=bop then incr(page_count)
  1749.   else bad_dvi('byte ',q:1,' is not bop');
  1750. @.byte n is not bop@>
  1751.   for k:=0 to 9 do count[k]:=signed_quad;
  1752.   if start_match then start_loc:=q;
  1753.   p:=signed_quad;
  1754.   until p<0;
  1755. if start_loc<0 then abort('starting page number could not be found!');
  1756. move_to_byte(start_loc+1); old_backpointer:=start_loc;
  1757. for k:=0 to 9 do count[k]:=signed_quad;
  1758. p:=signed_quad; started:=true
  1759. @* Reading the postamble.
  1760. Now imagine that we are reading the \.{DVI} file and positioned just
  1761. four bytes after the |post| command. That, in fact, is the situation,
  1762. when the following part of \.{DVIDOC} is called upon to read, translate,
  1763. and check the rest of the postamble.
  1764. @p procedure read_postamble;
  1765. var k:integer; {loop index}
  1766. @!p,@!q,@!m:integer; {general purpose registers}
  1767. begin post_loc:=cur_loc-5;
  1768. @.Postamble starts at byte n@>
  1769. if signed_quad<>numerator then
  1770.   print_ln('numerator doesn''t match the preamble!');
  1771. @.numerator doesn't match@>
  1772. if signed_quad<>denominator then
  1773.   print_ln('denominator doesn''t match the preamble!');
  1774. @.denominator doesn't match@>
  1775. if signed_quad<>mag then if new_mag=0 then
  1776.   print_ln('magnification doesn''t match the preamble!');
  1777. @.magnification doesn't match@>
  1778. max_v:=signed_quad; max_h:=signed_quad;@/
  1779. max_s:=get_two_bytes; total_pages:=get_two_bytes;@/
  1780. @<Process the font definitions of the postamble@>;
  1781. @<Make sure that the end of the file is well-formed@>;
  1782. @ When we get to the present code, the |post_post| command has
  1783. just been read.
  1784. @<Make sure that the end of the file is well-formed@>=
  1785. q:=signed_quad;
  1786. if q<>post_loc then
  1787.   print_ln('bad postamble pointer in byte ',cur_loc-4:1,'!');
  1788. @.bad postamble pointer@>
  1789. m:=get_byte;
  1790. if m<>id_byte then print_ln('identification in byte ',cur_loc-1:1,
  1791. @.identification...should be n@>
  1792.     ' should be ',id_byte:1,'!');
  1793. k:=cur_loc; m:=223;
  1794. while (m=223)and not eof(dvi_file) do m:=get_byte;
  1795. if not eof(dvi_file) then bad_dvi('signature in byte ',cur_loc-1:1,
  1796. @.signature...should be...@>
  1797.     ' should be 223')
  1798. else if cur_loc<k+4 then
  1799.   print_ln('not enough signature bytes at end of file (',
  1800. @.not enough signature bytes...@>
  1801.     cur_loc-k:1,')');
  1802. @ @<Process the font definitions...@>=
  1803. repeat k:=get_byte;
  1804. if (k>=fnt_def1)and(k<fnt_def1+4) then
  1805.   begin p:=first_par(k); define_font(p); print_ln(' '); k:=nop;
  1806.   end;
  1807. until k<>nop;
  1808. if k<>post_post then
  1809.   print_ln('byte ',cur_loc-1:1,' is not postpost!')
  1810. @.byte n is not postpost@>
  1811. @* The main program.
  1812. Now we are ready to put it all together. This is where \.{DVIDOC} starts,
  1813. and where it ends.
  1814. We use the UNIX facilty to pass in command line arguments here.
  1815. @^system dependencies@>
  1816. @p begin 
  1817. initialize; {get all variables initialized}
  1818. options; {set up all the options}
  1819. @<Open input file@>
  1820. @<Create error message file@>
  1821. @<Process the preamble@>;
  1822. @<Find the postamble, working back from the end@>;
  1823. in_postamble:=true; read_postamble; in_postamble:=false;
  1824. @<Count the pages and move to the starting page@>;
  1825. if not in_postamble then @<Translate up to |max_pages| pages@>;
  1826. final_end:end.
  1827. @ The main program needs a few global variables in order to do its work.
  1828. @<Glob...@>=
  1829. @!k,@!m,@!n,@!p,@!q:integer; {general purpose registers}
  1830. @ A \.{DVI}-reading program that reads the postamble first need not look at the
  1831. preamble; but \.{DVIDOC} looks at the preamble in order to do error
  1832. checking, and to display the introductory comment.
  1833. @<Process the preamble@>=
  1834. p:=get_byte; {fetch the first byte}
  1835. if p<>pre then bad_dvi('First byte isn''t start of preamble!');
  1836. @.First byte isn't...@>
  1837. p:=get_byte; {fetch the identification byte}
  1838. if p<>id_byte then
  1839.   print_ln('identification in byte 1 should be ',id_byte:1,'!');
  1840. @.identification...should be n@>
  1841. @<Compute the conversion factor@>;
  1842. p:=get_byte; {fetch the length of the introductory comment}
  1843. print('''');
  1844. while p>0 do
  1845.   begin decr(p); print(xchr[get_byte]);
  1846.   end;
  1847. print_ln('''')
  1848. @ The conversion factors |horiz_conv| and 
  1849. |vert_conv| are figured as follows: There are exactly
  1850. |n/d| \.{DVI} units per decimicron, and 254000 decimicrons per inch,
  1851. and |horiz_resolution| or |vert_resolution| characters per inch. Then we have to adjust this
  1852. by the stated amount of magnification.
  1853. @<Compute the conversion factor@>=
  1854. numerator:=signed_quad; denominator:=signed_quad;
  1855. if numerator<=0 then bad_dvi('numerator is ',numerator:1);
  1856. @.numerator is wrong@>
  1857. if denominator<=0 then bad_dvi('denominator is ',denominator:1);
  1858. @.denominator is wrong@>
  1859. horiz_conv:=(numerator/254000.0)*(horiz_resolution/denominator);
  1860. vert_conv:=(numerator/254000.0)*(vert_resolution/denominator);
  1861. mag:=signed_quad;
  1862. if new_mag>0 then mag:=new_mag
  1863. else if mag<=0 then bad_dvi('magnification is ',mag:1);
  1864. @.magnification is wrong@>
  1865. true_horiz_conv:=horiz_conv; horiz_conv:=true_horiz_conv*(mag/1000.0);
  1866. true_vert_conv:=vert_conv; vert_conv:=true_vert_conv*(mag/1000.0);
  1867. @ The code shown here uses a convention that has proved to be useful:
  1868. If the starting page was specified as, e.g., `\.{1.*.-5}', then
  1869. all page numbers in the file are displayed by showing the values of
  1870. counts 0, 1, and~2, separated by dots. Such numbers can, for example,
  1871. be displayed on the console of a printer when it is working on that
  1872. page.
  1873. @<Translate up to...@>=
  1874. begin while max_pages>0 do
  1875.   begin decr(max_pages);
  1876.   print('Page ');
  1877.   for k:=0 to start_vals do
  1878.     begin print(count[k]:1);
  1879.     if k<start_vals then print('.')
  1880.     else print_ln(' ');
  1881.     end;
  1882.   if not do_page then bad_dvi('page ended unexpectedly');
  1883. @.page ended unexpectedly@>
  1884.   repeat k:=get_byte;
  1885.   if (k>=fnt_def1)and(k<fnt_def1+4) then
  1886.     begin p:=first_par(k); define_font(p); k:=nop;
  1887.     end;
  1888.   until k<>nop;
  1889.   if k=post then
  1890.     begin in_postamble:=true; goto done;
  1891.     end;
  1892.   if k<>bop then bad_dvi('byte ',cur_loc-1:1,' is not bop');
  1893. @.byte n is not bop@>
  1894.   @<Pass a |bop|...@>;
  1895.   end;
  1896. done:end
  1897. @* System-dependent changes.
  1898. This section should be replaced, if necessary, by changes to the program
  1899. that are necessary to make \.{DVIDOC} work at a particular installation.
  1900. It is usually best to design your change file so that all changes to
  1901. previous sections preserve the section numbering; then everybody's version
  1902. will be consistent with the printed program. More extensive changes,
  1903. which introduce new sections, can be inserted here; then only the index
  1904. itself will get a new section number.
  1905. @^system dependencies@>
  1906. @* Index.
  1907. Pointers to error messages appear here together with the section numbers
  1908. where each ident\-i\-fier is used.
  1909.